I have below code for my routing for web forms and MVC.
MVC routing seems to be working fine but not the web form when I mix both.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("");
//MVC
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
//WebForms
routes.MapPageRoute(
"myPage",
"page/companyInfo/{*queryvalues}",
"~/company/details.aspx"
);
Do I need to write a IgnoreRoute statement for details.aspx page?
Change the order of the routes, the MVC route should be at the bottom as default is basically a catch-all. MVC process routes from top to bottom, if it finds a match, it stops looking and routes you to the matched route.
//WebForms
routes.MapPageRoute(
"myPage",
"page/companyInfo/{*queryvalues}",
"~/company/details.aspx"
);
//MVC
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Related
Situation
I'll build my URL like follow:
The overview page on / (at HomeController).
The detail page on /details/123 instead of /home/details/123 knowing that 123 always is a number (at HomeController).
An info page on /info instead of /home/info (at HomeController).
The logon page on /account/signin (at AccountController).
I've don't have more pages on my application.
Try 1
I've created two routes like follow:
routes.MapRoute(
name: "Home",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The problem is that the account page gives a 404. The other pages works.
Try 2
I've created one route like below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
On each method inside the HomeController added a the Route attribute with the correct URL.
The problem is now that the overview and logon pages works the others gives now a 404.
Question
How could I remove the home directory from the URL with ASP.NET MVC?
To use attribute routes you need to add
routes.MapMvcAttributeRoutes();
to your RouteConfig.cs file.
It should go before your default route.
I'm having trouble doing what I want to achieve when doing routing in ASP.NET MVC.
What I want to do is the following:
When typing http://localhost/MyWebsite/, I want to redirect to http://localhost/MyWebsite/Login/ (The redirection to the action Index is implied here)
When typing http://localhost/MyWebsite/MyAction, I want to redirect to http://localhost/MyWebsite/Login/MyAction
Point 1 was achieved using the following lines in the file RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
But I cannot accomplish point 2. Note that the user does not come from another action in a controller, but actually types the address in his browser.
Thanks for your help.
since /MyWebsite/Login and /MyWebsite/MyAction both have two segments in the URL, they're both matching your defined Route.
You can use a Route Constraint to only match /MyWebsite/Login to your first point, followed by a modified second route mapping:
routes.MapRoute(
name: "Default",
url: "MyWebsite/Login/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Actions",
url: "MyWebsite/{action}/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
I have home controller and Index action method. The below url works
http://localhost/home/index
Will it be possible to make it work like below
http://localhost/index.aspx
I am trying below code in Global.asax but does not works
routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");
Route Config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");
routes.MapRoute(
name: "Customized",
url: "{action}",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Reports",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Reports", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
}
You should use MapRoute() instead of MapPageRoute(), as you are still referring to an MVC controller/action:
routes.MapRoute(
name: "Default2",
url: "index.aspx",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
PS: Remember to register the new route before other ones which may eventually interfere with it.
You can't MapPageRoute for non static content as you are doing. If you wan't to hide the Controller for your route the Customized already do this. If you are mixing MVC + WebForms you should fallow this guide to se how config your routes.
How to: Define Routes for Web Forms Applications
I have set up the following structure for my website. (see link below)
http://postimg.org/image/ut4klihrp/
localhost:62540 Goes to the index page of the core
localhost:62540/www/Home Goes to the index page of the WWW
localhost:62540/cms/Home Goes to the indec page of the cms
I basically want the 'default' route (localhost:62540) to go to my WWW project. How can i do this or does anybody know a tutorial were the principle for this get explained? Or is this not possible since i use the area method.
Eventually i want to remove the view and controller from the core project.
Route config in www :
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);
You can specify the default area in the defaults:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = "www", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);
I'm trying to get an XML RPC service going as illustrated in the following article:
http://www.cookcomputing.com/blog/archives/Implementing%20an%20xml-rpc-service-with-asp-net-mvc
Everything works great, except the routing. It is a similar problem to what has been discussed in this SO question MVC route conflicts with service route
My code for RegisterRoutes look like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.Add(new Route("wlw/publish", new WLWRouteHandler()));
}
When I put this line
routes.Add(new Route("wlw/publish", new WLWRouteHandler()));
before MapRoutes I can access the service but my normal routes does not work. I tried adding a fourth parameter:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = "regex-for-!=-wlw" }
);
but then I get a 403 The Web server is configured to not list the contents of this directory error.
What am I doing wrong?
A good solution for this can be found here : http://weblogs.asp.net/jasonconway/archive/2009/10/23/include-and-exclude-constraints-in-asp-net-mvc.aspx
I changed MapRoute to the following:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "home", action = "index", id = "" },
new { controller = new ListConstraint(ListConstraintType.Exclude, "wlw") }
);