3

When I go to my home page

http://localhost:5119/  

it redirects to

http://localhost:5119/Home

(that is what shows in my browser url bar, I want it to not show /Home)

I understand that is where my default route goes but what I can't figure out what is causing my URL line to be rewritten in the browser. The default example in Visual Studio 2012 does not have this issue when you go to the base URL. I'm attaching a picture of my route debug (that does not seem to help me, but may have some value).

Thanks, -Peter

Adding This After. It is relevant parts of route code

        // HOME
        int currentYearInt;
        Int32.TryParse(currentYear, out currentYearInt);
        routes.MapRoute("HomeRouteAll", "Home/{yearInt}",
                  new
                  {
                      /* Your default route */
                      controller = "Home",
                      action = "Index"
                  });



        // DEFAULT ROUTE
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

enter image description here

Peter Kellner
  • 14,748
  • 25
  • 102
  • 188

1 Answers1

1

You are seeing this because of the route Home/{year} above your default route that has a default value for the {year} parameter.

The route engine makes the following decisions:

  1. Start at top of route list
  2. Look for something that matches what your route values (controller = "Home", Action="Index").
  3. At the first match, return and that is your URL.

Since you have a matching controller (Home) and action (Index) as well as a default value for the year parameter, the route engine is matching to the route Home/{year}, thus giving the url http://domain.com/Home.

The quick fix would be either a) make year not have a default value (Home/2013), b) move whatever that is over to a different controller (NewName/{year}), c) move that to a different action (NewIndex/{year}) or d) update your default route to use the year parameter instead of id

routes.MapRoute(
                "Default",
                "{controller}/{year}/{action}",
                new {controller = "Home", action = "Index", year = 2013});

EDIT

I am not really sure what you have as far as the tryParse stuff in your route definitions, but in my testing, this seemed to accomplish what you are wanting to do:

routes.MapRoute(
                name: "Test",
                url: "Home/{year}",
                defaults: new { controller = "Home", action = "Index"}, 
//this line causes the year to be an integer
constraints: new { year = @"\d+" }
                );

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

Behaviors:

http://domain.com/ -> calls Home controller, Index action with a null value for the year parameter (no redirect to Home/2013)

http://domain.com/Home/2010 -> calls Home Controller, Index action with 2010 for the year parameter

http://domain.com/Home/ -> call Home Controller, Index action with null value for the year.

If you define a default year in the first of the two routes, then going to http://domain.com/Home will call Home controller, Index action with 2013 for the year and the redirect will still not occur.

Lastly, I suspect your Home/Index action looks something like this:

public ActionResult Index(int year){...}

If when you hit the Home/Index action, you want to automatically have the 2013 populated, then change your int to a nullable parameter and do it there rather than your routes.

public ActionResult Index(int? year){
    if(!year.hasValue){
        year = 2013;
    }

By performing this logic here instead of the route, you should prevent the redirect to Home/ as you have no matching {year} parameter.

Tommy
  • 39,592
  • 10
  • 90
  • 121
  • Hi Tommy, I tried all the doors, liking choice A the best. I changed my home route to // HOME int currentYearInt; Int32.TryParse(currentYear, out currentYearInt); routes.MapRoute("HomeRouteAll", "Home/{yearInt}", new { /* Your default route */ controller = "Home", action = "Index" }); but I still get the behavior that it leaves me by default at .../Home – Peter Kellner Apr 19 '13 at 21:34
  • BTW, I don't want to change my defualt route because I count on that for other routes that I am not explicit about. – Peter Kellner Apr 19 '13 at 21:34
  • @PeterKellner Can you post that stuff in your question for formatting reasons? – Tommy Apr 19 '13 at 21:43
  • I've always wonder about that in SO. Is that the proper protocol? I've always hesitated to change my original question because then the comments and answers don't make sense anymore. I'll try though. – Peter Kellner Apr 19 '13 at 21:56
  • You can edit your question if it adds to the discussion. Typically, I will use two asterix and type **EDIT** to help identify that it was new information – Tommy Apr 19 '13 at 21:57
  • any more thoughts on this one Tommy? – Peter Kellner Apr 21 '13 at 21:37
  • I have given some test routes in my edit to my answer and explained why you are seeing your URL go to Home/. Basically, with the way you have your home/index action setup with your current routes, this will happen b/c the home/{year} route matches first. Take a look at the test routes and see if that helps you decide on how to proceed with your routes/controller and action names. – Tommy Apr 22 '13 at 15:01
  • Tommy, I totally understand why it should work. The sample MVC4 project in VS works. My code appears exactly the same. Obviously I have some gotcha (in this case got me) in there. I'll keep digging). I do have int? and I have even tried stripping all the routes except my default route, removing all my extra global.asax stuff and just made an empty controller index(). Still, same behavior. any more ideas, feel free to suggest. – Peter Kellner Apr 23 '13 at 00:07
  • Turns out my problem was cassini developer browser. Originally, the problem was, as Tommy suggests I did not have int? as a parameter for the year. After I fixed that, it did not seem fixed because cassini kept redirecting to /Home. when I switched to iis express or iis, it worked as expected. Thanks Tommy for all your help. – Peter Kellner Apr 24 '13 at 16:28