Default values for route parameters

Imported from the CodePlex archive for reference purposes. Support for MvcCodeRouting has ended.

Commented on

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

Commented on link

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.

Commented on link

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

Commented on link

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

Commented on link

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

Commented on link

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

Commented on link

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? 

Commented on link

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

Commented on link

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" }); 

Commented on link

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

Commented on link

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

Commented on link

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

Commented on link

Yes, it does..

Commented on link

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

Commented on link

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.

Commented on link

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

Commented on link

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