2

I'm using a default route, so that I don't need to specify the controller.

routes.MapRoute(
    "Default", 
    "{action}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

With this, I can create URLs like myapp.com/Customers rather than myapp.com/Home/Customers

When I test locally, everything is fine. When I upload a live version, any links generated with Html.ActionLink are empty. I know I'm using Html.ActionLink correctly, because it works fine locally:

//                   Title                 Action      Controller
<%: Html.ActionLink("Manage My Settings", "Settings", "Home") %>

I've removed all routes but the default, tried specifying ActionLink with and without the controller etc. I even tried reverting to having the controller in the route, e.g:

"{controller}/{action}/{id}"

Nothing works live. Everything works locally. Going slightly mad.

UPDATE:

OK, made a strange discovery. I actually had another optional UrlParameter after id, called page. I stupidly didn't include it in the example because I thought it made no difference. If I take it out, things seem to work.

So, actually, this works:

routes.MapRoute(
   "Default", 
   "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

and this works!

routes.MapRoute(
   "Default", 
   "{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

but this does Not work

routes.MapRoute(
   "Default", 
   "{action}/{id}/{page}", 
    new { controller = "Home", action = "Index", 
    id = UrlParameter.Optional, page = UrlParameter.Optional }
);

Why Not?

12
  • 1
    on wich version of IIS you have been deployed application the same as localy? Commented Jan 18, 2012 at 14:08
  • IIS6 on the server, and locally its whatever internal server Visual Studio 2010 runs, which I believe is IIS 7.5 Commented Jan 18, 2012 at 14:23
  • this is the problem look in my answer below Commented Jan 18, 2012 at 14:25
  • 1
    Has it ever worked on IIS6 before? Before you dismiss Serghei's answer you should follow those steps. MVC doesn't work on IIS 6 out of the box. Commented Jan 18, 2012 at 15:08
  • 1
    Can you clarify what you mean by "empty"? Do the links look like this? <a href="">Manage My Settings</a> Commented Jan 18, 2012 at 15:09

2 Answers 2

5

Found the answer! There's a bug in MVC3 when using two consecutive optional UrlParameters, as detailed by Phil Haack, here routing-regression-with-two-consecutive-optional-url-parameters

You need to first declare a version of the route with only one optional parameter. So

routes.MapRoute(
    "Default", // Route name
    "{action}/{id}", // URL with ONE parameter
    new { controller = "Home", action = "Index", 
    id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "Default_with_page", // Route name
    "{action}/{id}/{page}", // URL with TWO parameters
    new { controller = "Home", action = "Index", 
    id = UrlParameter.Optional, page = UrlParameter.Optional } 
    // Parameter defaults
);

Seems really obvious now. If I'd actually included all the details I'm sure Serghei or someone else would have seen the problem, so thanks for all the help guys!

Sign up to request clarification or add additional context in comments.

2 Comments

O M G. Thank you so much. I spent an hour trying to figure this out. I thought it was caused by a sub domain.
1

look at the article in which is explayned how to deploy ASP.NET MVC Application on IIS6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.