This project is read-only.

Default values for route parameters

Feb 4, 2012 at 3:56 PM
Edited Feb 4, 2012 at 4:05 PM

Hi all,

I have a controller action (Index) with 2 optional parameters. One of them is string and the other one is int.

public ActionResult Index(string stringVar = nullint intVar = 1)

How can I take them from route? I tried [FromRoute] in the parameter definition and [RequireRouteParameters] in some ways, but as long as I try to reach localhost/MyApp/Controller url I get a 404 error (Not found). As long as I remove all the mvccoderouting related properties the whole app works but I don't have any routing for this controller (obviously). What can I do in order to solve this problem?

Thanks in advance for your help

Feb 4, 2012 at 4:11 PM

All you need to do is decorate the parameters with [FromRoute]. 
[RequireRouteParameters] is used for overloaded actions methods that do not have any other disambiguation attribute (e.g. [HttpGet], [HttpPost], etc.)

Note that when using [FromRoute] on the default action (Index) the route will include the Index segment, e.g. Controller/Index/{stringVar}/{intVar}, if what you want is to exclude the Index segment you need to use the [CustomRoute] attribute like this:


[CustomRoute("{stringVar}/{intVar}")]
public ActionResult Index([FromRoute]string stringVar = null, [FromRoute]int intVar = 1) {
   ...
}

Let me know if this helps.

Feb 4, 2012 at 4:16 PM

Thanks for your reply..I've tried that, but with no luck. I still get the 404 error message.

Feb 4, 2012 at 4:18 PM

Can you show me the route definition (in routes.axd) ?

Feb 4, 2012 at 4:20 PM

How can I see that? I was trying to find this file too..It seems silly but I can't find it..

Feb 4, 2012 at 4:27 PM

If you installed MvcCodeRouting using NuGet then it should be in the web.config, else you have to add this:


   <system.web>
      <httpHandlers>
         <add path="routes.axd" verb="GET,HEAD" type="MvcCodeRouting.RouteDebugHandler, MvcCodeRouting"/>
      </httpHandlers>
   </system.web>
   <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <handlers>
         <add name="MvcCodeRouting.RouteDebugHandler" path="routes.axd" verb="GET,HEAD" type="MvcCodeRouting.RouteDebugHandler, MvcCodeRouting"/>
      </handlers>
   </system.webServer>

Then visit http://localhost/MyApp/routes.axd

Feb 4, 2012 at 4:37 PM

the portion is already in my web.config but I don't have the routes.axd file. My web.config has <compilation debug="true"/> and I've published it to iis with debug configuration. Am I doing something wrong? 

Feb 4, 2012 at 4:39 PM

There is no routes.axd file, just navigate to ~/routes.axd

Feb 4, 2012 at 5:06 PM
Edited Feb 4, 2012 at 5:07 PM

OK, I got it. With 

[CustomRoute("{stringVar}/{inVar}")]
public ActionResult Index([FromRoute]string stringVar= null, [FromRoute]int intVar= 1)

I get the following rule in routes.axd

routes.MapRoute(null,"{stringVar}/{intVar}", new { controller = @"Controller", action = @"Index", intVar= UrlParameter.Optional }, new { intVar = @"(0|-?[1-9]\d*)?" }, new[] { "MyApp.Controllers" }); 

Feb 4, 2012 at 5:12 PM

That's weird, what is category and page doing there?

Feb 4, 2012 at 5:14 PM

That was my mistake. I've corrected the post and fixed it in my project too but with no change, still 404.

Feb 4, 2012 at 5:16 PM

For some reason stringVar is not considered optional, does it work if you visit ~/foo ?

Feb 4, 2012 at 5:17 PM

Yes, it does..

Feb 4, 2012 at 5:19 PM

It does that for every optional string route variable. I tried it with others too.

Feb 4, 2012 at 5:30 PM

OK, stringVar should be optional, but when set to null it doesn't work, if you set to any other value e.g. stringVar ="" it works. I'll try to fix this if possible.

Feb 4, 2012 at 5:36 PM
Edited Feb 4, 2012 at 6:36 PM

It works with "" .I thought that I have tried that, but now it worked. Thanks for your help!

Feb 5, 2012 at 8:29 PM

This issue was added to the tracker and fixed for the next release.

Marked as answer by maxtoroq on 10/15/2013 at 10:06 AM