Hide params in url by asp.net mvc routing - asp.net-mvc

I have an asp.net mvc 2 application. I get a page with the url http://localhost/Object/ChangeObject/108?MtRid=216584. I want to route it like this: http://localhost/Object/ChangeObject.
How to write a route for this?

There are more than one way to do this:
Try to modify the methode "application_beginrequest" of "Global.asax" This method is called every time some request is made to the website.
Take a look at this example
URL Routing - By adding custom URL Route mapping rules before the default one as described in this article

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

Related

When are default route values used?

I'm confused about default routing values. Here's the default route in an MVC app:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I don't understand why it specifies a default value for, say, {controller}, because it seems to me that this route won't ever be used unless the user requests a url like /xyz/dosomething/123, and in that case the controller is simply xyz, and we don't need the default value.
So, with a route like this, when would the default controller and action values ever be used?
It would be used if you don't specify them in the url : http://whatever.com will be treated as http://whatever.com/Home/Index.
The default controller and action will be used when the page / is requested, i.e. when someone browses to just your domain address, e.g. http://www.mydomain.com.

custom url in asp .net mvc

I am new to ASP.NET-MVC and I am trying to create a simple blog application. I want to use a custom url for the blog's details pages.
Right now the url for blog's details pages are the standard 'localhost/Blog/Details/3', but I want to actually use the url 'localhost/Blog/2012/06/blog-title', basically using 'localhost/Blog/{year}/{month}/{BlogTitle}'
I have tried looking on the internet but I do not understand how to do this and am not able to get a simple tutorial on how either.
You can create a new route in Global.asax.cs as below,
routes.MapRoute(
"Post", // route-name
"Blog/{year}/{month}/{BlogTitle}", // format
new { controller = "Books", action = "Post" }, // controller & action
new { year = #"\d{4}", month = #"\d{2}" } // constraints
);
You have to map a custom route
routes.MapRoute(
"Default", // Route name
"Blog/{action}/{month}/{BlogTitle}", // URL with parameters
new {controller ="MyController"}
);
Any url of type localhost/Blog/text/text/text will map to this route
this url will call MyController.Action(month,BlogTitle)
Make sure to put the more restrictive routes first becouse the first route that matches the url will be considered (from top to bottom)

ASP.MVC 3 routing : how to get url with default action included?

Suppose I have the following routing
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
Now, when you generate an url using Url.Action("Index","MyController") you will get as expected : /MyController
But in one exceptional case, I would like to get the full url /MyController/Index
(without changing the routing)... does anyone know if this is possible?
It is possible. But you need to modify the routing.
Create an own routing class that derives Route
Override the GetVirtualPath() method to include /index for the pages that needs it.
Configure the default route using your routing class instead.
I am afraid this is not possible. And it shouldn't matter as both urls will resolve to the same controller action.

How does MVC routing understands the URL?

Global.asax.cs has the following code on initialization:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
What I'm asking is, how does it know that what it gets for "{controller}" will be the name of the Controller class to be invoked? Are there tokens defined somewhere? if so, can I list them?
If I define additional tokens (like "{lang}") will it assume they are additional parameters?
(I'm developing a custom URL rewrite/redirect handler, and I need it to work with MVC...)
What is the most practical way to define custom patterns and "aliases" for URLs?
The Mvc runtime has the controller and action tokens hardcoded. In addition there is also "area" but thats about it.
#TDaver If I define additional tokens (like "{lang}") will it assume they are additional parameters?
yes. If you define, for instance, a parameter like lang, it wil detect it. Think about like that, it will be the querystring field called lang of the page. and you can create a route for a pretyy url. Like below;
routes.MapRoute(
"Default", // Route name
"{lang}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
so the url will be like ; http://example.com/en/home/about
Also, the most important part of routing is to understand that the routes will be picked by order. for instance, if you have multiple routes matching your current request, the first route will be picked by MVC Framework.
I reccomend you to have a look at phil haccked's RouteDebugger
Also you can create route constraints for advanced routing options as well.

/Mappings/Index is found but not /Mappings with ASP.NET MVC

Just struggling with a simple issue with ASP.NET MVC. I have a list of views, each view associated with an Index.aspx view being associated by default with /MyView.
Yet, for some reason I have 1 view named /Mappings that does not work (404 resource is not found) whereas the explicit path /Mappings/Index works.
I have the default route settings as provided by the default ASP.NET MVC sample
routes.MapRoute(
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
And, the default Index works for the other views of the same webapp.
Any idea what could be wrong here?
You have to define default action if it is not provided:
route.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { action = "Index" } // Default action if not provided
);
EDIT:
Look at this link:
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
You can use this debugger to test your routing.

Resources