Mixing ASP.NET and MVC routing - c#

I thought I could have friendly URLs for all routes in my mixed ASP.NET + MVC application, but it is not working as I expect. Here is my routing definition setup:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Design-Fancy", "Design/Fancy/{*queryvalues}", "~/Design/example10.aspx", true);
routes.MapPageRoute("Design-Simple", "Design/Simple/{*queryvalues}", "~/Design/example5.aspx", true);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
While this works to route to the *.aspx pages, Any Razor action tags on the same page that are defined for example as "Home" for the controller and "About" for the Action actually are rendered in the page source as 'http://..../Design/Fancy?action=About&controller=Home'. So, this breaks all the navigation menu URLs, etc. I must be doing it wrongly!

Here is the solution I settled on. I installed the NuGet Microsoft.AspNet.FriendlyUrls package. Then I named the .aspx page with a page name that would look good without the extension. Then I set up the routing as follows:
public static void RegisterRoutes(RouteCollection routes)
{
FriendlyUrlSettings aspxSettings = new FriendlyUrlSettings();
aspxSettings.AutoRedirectMode = RedirectMode.Off; // default=Off
routes.EnableFriendlyUrls(aspxSettings);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapPageRoute("Design-Fancy", "Design/Fancy/{*queryvalues}", "~/Design/Fancy.aspx", true);
routes.MapPageRoute("Design-Simple", "Design/Simple/{*queryvalues}", "~/Design/Simple.aspx", true);
}
This gives the effect I wanted, and works with my MVC routing, and results in routing to .aspx pages while removing the .aspx extension.

Related

How Can I change the route config to the custom route?

I tried to change and optimize my website URL to the SEO friendly Url. I mean I change the url Like ~/Home/Contact to ~/contact and etc. I change the ~/Home/Index URL to ~/home as well.
When I run my website because I add the attr [Route("~/home")] to my index action application can't find my default route.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
I don't know how can I change my MapRoute to my new SEO Friendly URL.
I don't want to loss my mvc URL Pattern as well
You can use this class in App_Start folder:
public static class RoutingConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
#region IgnoreRoutes
routes.IgnoreRoute("Content/{*pathInfo}");
routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{resource}.ico");
routes.IgnoreRoute("{resource}.png");
routes.IgnoreRoute("{resource}.jpg");
routes.IgnoreRoute("{resource}.gif");
routes.IgnoreRoute("{resource}.txt");
#endregion
routes.LowercaseUrls = true;
routes.MapMvcAttributeRoutes();
// AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults:
new
{
controller = MVC.Home.Name,
action = MVC.Home.ActionNames.Index,
id = UrlParameter.Optional
},
namespaces: new[] {$"{typeof (RoutingConfig).Namespace}.Controllers"}
);
}
and use this in Global.asax.cs Application_Start method.
RoutingConfig.RegisterRoutes(RouteTable.Routes);
I used from T4MVC nuget package.

ASP.NET MVC routing add special convention mysitye/string

I am new to ASP.NET MVC. I have a web page with default routing:
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 }
);
}
I have been working on developing an URL shortener that creates 5 character strings in base 32 (A-Z 0-9). My idea is to have the default routing in ASP.NET MVC and add a special case for
// this is the random code generated by my application
www.mypage.com/ASD12
How can I add this exception to my routing and always make URLs (mypage.test/code) land on a specific controller action?
public class CodeController : Controller
{
public async Task<ActionResult> Index(string code)
{
//do things here
}
}
Thank you very much
After searching more,
routes.MapRoute(
name: "specialConvention",
url: "{id}",
defaults: new { controller = "code", action = "Index" }
);
That is the code I was looking for.

How To Set Default Action on Mvc Startup [duplicate]

How do I set Default Controller for my ASP.NET MVC 4 project without making it HomeController?
How should I setup a default Area when the application starts?
the best way is to change your route. The default route (defined in your App_Start) sets /Home/Index
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
as the default landing page. You can change that to be any route you wish.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Sales", action = "ProjectionReport",
id = UrlParameter.Optional }
);
Set below code in RouteConfig.cs in App_Start folder
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional });
}
IF still not working then do below steps
Second Way :
You simple follow below steps,
1) Right click on your Project
2) Select Properties
3) Select Web option and then Select Specific Page (Controller/View) and then set your login page
Here, Account is my controller and Login is my action method (saved in Account Controller)
Please take a look attached screenshot.
I didn't see this question answered:
How should I setup a default Area when the application starts?
So, here is how you can set up a default Area:
var route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens = new RouteValueDictionary(new { area = "MyArea" });
In case you have only one controller and you want to access every action on root you can skip controller name like this
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);

Understanding routing

I have trying to change the routing of my asp.net project. I want Login controller to load on startup of my project rather than any other controller.
So, I have added LoginDefault routemap in existing routes in asp project
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LoginDefault",
url: "{controller}/{action}",
defaults: new { controller = "UserManagement", action = "Login" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This loads up the Login controller right but in doing so the default routing is not executed. After login, dashboard controller is invoked but "Index" is added to each URL as below.
http://localhost:49799/Dashboard/Index
This has effected my URL and other Ajax call and this doesn't look neat. Before adding the LoginDefault the URL would be
http://localhost:49799/Dashboard
I would like to achieve this. If any other way is possible that too will be fine.
Thank you
try this
routes.MapRoute(
name: "LoginDefault",
url: "{controller}",
defaults: new { controller = "UserManagement", action = "Login" }
);

How to set Default Controller in asp.net MVC 4 & MVC 5

How do I set Default Controller for my ASP.NET MVC 4 project without making it HomeController?
How should I setup a default Area when the application starts?
the best way is to change your route. The default route (defined in your App_Start) sets /Home/Index
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
as the default landing page. You can change that to be any route you wish.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Sales", action = "ProjectionReport",
id = UrlParameter.Optional }
);
Set below code in RouteConfig.cs in App_Start folder
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional });
}
IF still not working then do below steps
Second Way :
You simple follow below steps,
1) Right click on your Project
2) Select Properties
3) Select Web option and then Select Specific Page (Controller/View) and then set your login page
Here, Account is my controller and Login is my action method (saved in Account Controller)
Please take a look attached screenshot.
I didn't see this question answered:
How should I setup a default Area when the application starts?
So, here is how you can set up a default Area:
var route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens = new RouteValueDictionary(new { area = "MyArea" });
In case you have only one controller and you want to access every action on root you can skip controller name like this
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);

Categories

Resources