3

I am trying to figure out how to route my application to a default controller/task/id when none is specified in a request.

Here is my one routing instruction...

 routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new
    {
     controller = "LML",
     action = "TaskLibrary",
     id = 7
     //id = UrlParameter.Optional
    } // Parameter defaults
   );

Using this, if I enter in 'http://mywebsite/', the proper controller/action/id is called. However, I would like the URL to reflect this. Rather the URL remains untouched from what I entered.

Using routing, is there a way to affect the URL so that it redisplays synced with the controller/action/id it is showing by default? Or do I have to create some sort of redirect action?

1
  • 1
    please tag with asp.net next time ---- mvc is a generic term, it is not asp specific. Commented Oct 28, 2010 at 4:44

2 Answers 2

4

Routing is about mapping a request to an action, not about redirecting.

You could change your default route parameters to default to another action that simply redirects to "LML/TaskLibrary/7"

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

2 Comments

Thanks for the quick response. I am just learning MVC. So you are suggesting the default route params hit a controller action that simply redirects to a default URL? Sorry if obvious. Still learning!
Keep in mind that default route parameters are used when there's no match on the request. Using your route, the following requests are the same - "/", "/LML", "/LML/TaskLibrary" and "/LML/TaskLibrary/7". It's up to you to decide the best landing page (no parameters so use all defaults) for your site.
2

I'm new to ASP.NET MVC 4, and often like to see where the change can be made. So, for those who'd like to see the previous answer in code...

In RouteConfig.cs

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "LML", action = "TaskLibrary", id = 7 }
               );

The main difference from the original example, is the addition of "defaults:".

I hope this helps someone!

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.