I have a bigger project with about 9 controllers. Now at the end of the project the url requeirement change. How to deal best with this situation - renaming the controllers seems just too cumbersome ... I need to change all links in servercode and javascript
Your problem can be solved by changing your existing routes. In your global.asax you'll find a code fragment like this
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
That maps an URL '/Controller/Action/Id' to Controller, Action and Id. You can provide routes like this
routes.MapRoute(
"RefactoredRoute", // Route name
"SomeChangedURLBase/{action}/{id}", // URL with parameters
new { controller = "Controller", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
to route requests to /SomeChangedURLBase... to be handled by Controller.
Be aware that these routes should be registered before the default route to avoid that links generated in views point to the default route and generate the old URL.
you could change the routings in global.asax
just change the method RegisterRoutes
here you can find some more informations.
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
cheers
Related
I want to assign a route in asp.net mvc application.
What i have is a Measurement Controller. I have 3 types of Measurement in the business scenario.
Blouse
Lhenga
Pardi
Due to which i wanted the url to be like Measurement/Create/Lhenga
Just like this, I want to create Measurement/Create/Blouse and Measurement/Create/Pardi routes.
Although I know I will have to write a route in RouteConfig.cs class.
I have written
routes.MapRoute(
"MeasurementRoute",
"{controller}/{action}/{type}/"
);
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new {
controller = "Contact", action = "Address"
});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home", action = "Index", id = UrlParameter.Optional
}
);
}
Every MVC application must configure (register) at least one route, which is configured by MVC framework by default
You can also configure a custom route using MapRoute extension method. You need to provide at least two parameters in MapRoute, route name and url pattern. The Defaults parameter is optional.
You can register multiple custom routes with different names. Consider the following example where we register "Contact" route.
As shown in the above code, URL pattern for the Contact route is Contacts/{id}, which specifies that any URL that starts with domainName/Contacts, must be handled by ContactController. Notice that we haven't specified {action} in the URL pattern because we want every URL that starts with Contact should always use Index action of ContactController. We have specified default controller and action to handle any URL request which starts from domainname/Contacts.
MVC framework evaluates each route in sequence. It starts with first configured route and if incoming url doesn't satisfy the URL pattern of the route then it will evaluate second route and so on. In the above example, routing engine will evaluate Contact route first and if incoming url doesn't starts with /Contacts then only it will consider second route which is default route
I have to redirect non-www urls to use www. I have a choice of doing this in IIS 7 or code this logic in my ASP.Net application.
In terms of portability I would've thought writing this in the application itself might be better.
Is there a preferred method for achieving this or is it just preference?
When you do this in IIS7, all it does is modify your Web.Config files for you. I would have said that that was the conventional way of doing things.
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
I think, it is more rational to do this in the application. For example we have it defined like that:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"User", // Route name
"u/{user}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Hence we have plenty of flexibility and can have specific tokens in our links, etc. Yet if there was no need for that - we would use IIS base. So, I guess - whatever tickles your pickle here :)
Getting this error, everytime my home/INdex loads in my MVC3 app on Server 2008.
System.Web.HttpException: The file '/StudentPortal3G/Home.mvc.aspx' does not exist.
Tried all of this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Default2", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults );
);
routes.MapRoute(
"Default3", // Route name
"{controller}.mvc.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults );
);
Views\Home\Index.aspx exists.
IIS7 is not supposed to need the .* handler.
Do I need to set the aspx handler to not check existance of file? But the file exists?
If this is the answer, how do I set it on iis7, I wasn't able to find that to try it.
Isn't there a way to do it in the handlers section of the web.config?
Again I found a few hint's but I'm not quite getting it.
Thanks,
Cal-
Did you add appropriate Views? In this case, you should have Views\StudentPortal3G\ (assuming that StudentPortal3G is a Controller) directory with Home.aspx inside and it maps to /StudentPortal3G/Home
Why are you trying to load Home.mvc.aspx at all is beyond me. I'd suggest you fire up RouteDebug to see which rules you are missing...
I've got glimpse.
it's highlighting an empty line on the routes tab
Match Area Url Data constraints DataTokens
True Root -- --
Locally it seems cassini doesn't properly emulate a virtual directory so changing from localhost to localhost/site doesn't seem to grant any additional testing insight.
Local testing
cassini (windows 7 (32 and 64 bit are available))
doesn't seem to use or allow integrated
deploy environments
IIS7 Integrated
Tried a http routing setting to push / to /site however I ran into trailing slash issues (/site would route to /site/site, while /site/ would work as expected
web.config
tried a few ways of setting runAllManagedModules=false with various errors
using <remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /> that was linked
tried
I need /site (/~) requests to go to ~/Default.aspx
would love for a way to have / go there as well.
How do I do it?
To enable Default.aspx as the default url you just need to remove your route handling so that there's no catch-all route.
By default all MVC 3 projects have:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This is a catch-all route that will match everything you pass in your url. If you remove this and no path is defined then it will use whatever you set up as your default page in your web.config.
To map /site to Default.aspx you just need to add a special route:
routes.MapPageRoute("SitetoDefault", "site", "~/Default.aspx");
This will see the /site and route it to your default page.
You'll have to be careful when adding any other routes to your routing table. All of them will have to have a controller defined or you'll have to add a routing constraint. Something like this:
//controller defined
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
//route constraint
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}, // Parameter defaults
new { controller = "^(products)|(account)|(home)$" }
);
Here is more detail on creating more complex routing: http://www.asp.net/mvc/tutorials/creating-a-custom-route-constraint-cs
If I create an ASP.NET Web Application project and then add an ASP.NET MVC 2 to it using the default routes defined like so
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { action = "Index", id = UrlParameter.Optional }
);
The Session object is NULL when I try and access it in the action methods of the controllers. If I change my routes to this.
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { action = "Index", id = UrlParameter.Optional }
);
Everything works just fine. For whatever reason having the .aspx extension allows for session to be used, but the later doesn't. I'm using .NET 3.5 for everything.
Any ideas??? Thanks!
The solution is to add runAllManagedModulesForAllRequests="true" to the configuration>system.webServer>modules tag in the web.config.