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.
Related
I have a controller called Search. A normal url would be the following:
localhost:44351/<ClientName>/Search/ByCity
This would hit my ByCity action within my SearchController.
Now however, a url such as the following example, would also need to hit an action within the SearchController:
localhost:44351/<ClientName>/Search/Pharmacy/ByCity
I need to somehow tell my SearchController, if the url contains "Pharmacy/ByCity", to go to the ByCity action.
I've tried using the routing attribute, but my app still hits my old Pharmacy action instead.
In my RouteConfig, I have this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
Then, in my SearchController, I have this:
public virtual ActionResult Pharmacy()
{
//this is an existing action, which gets hit, even when I type in "Pharmacy/ByCity", which is not what I want to happen.
}
[Route("Pharmacy/ByCity")]
public virtual ActionResult ByCity()
{
//this never gets hit
}
Any idea how to have a url containing "Pharmacy/ByCity" to hit my "ByCity" action, rather than "Pharmacy"?
Thanks
It is possible to achieve with the conventional route by set up like below:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Pharmacy",
url: "{clientname}/{controller}/Pharmacy/{action}",
defaults: new { controller = "search" }
);
routes.MapRoute(
name: "Search",
url: "{clientname}/{controller}/{action}",
defaults: new { controller = "search", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Routes are accessed depending on their Order in the routing table.
For conventional routing (RouteConfig.cs), you could add your specific route before the default route.
Remove your Route[] attributes in the controller
Use the code below for RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// add your specific route, before the default route
routes.MapRoute(
name: "SearchByCity", // random name
url: "Search/Pharmacy/ByCity",
defaults: new { controller = "Search", action = "ByCity" }
);
// this is the default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
If you want to use Attribute Route, follow steps below.
Remove the default route in RouteConfig.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
Then use the controller below, we used RoutePrefix for the controller, and Route for the actions.
[RoutePrefix("Search")]
public class SearchController : Controller
{
[Route("Pharmacy")]
public virtual ActionResult Pharmacy()
{
return View("index");
}
[Route("Pharmacy/ByCity")]
public virtual ActionResult ByCity()
{
return View("index");
}
}
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.
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" }
);
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.
I have this RegisterRoute function
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("JsActionRoute", JsAction.JsActionRouteHandlerInstance.JsActionRoute);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Where the JsActionRoute is a route like this.
public static readonly Route JsActionRoute = new Route("JsAction",
new JsAction.JsActionHandler());
I want that all links to JsAction/ should be handled by my coustom route.
Now when simply calling #Html.ActionLink, Mvc3 creates a link that is related to JsAction, and I can't understand why.
#Html.ActionLink("About","About") -> JsAction?Action=About&Controller=Index
Routes are evaluated in the same order as they are registered. You could use the RouteLink to explicitly specify a route name:
#Html.RouteLink("About", "Default", new { action = "About" })
If you inverse the order of definitions then links will generate correctly but your custom route will not be hit when requesting the JsAction/ url since there is nothing to disambiguate this url from the default route.
You will have to rethink your routes structure so that there are no such conflicts. You could use constraints. Remember that the default route is very eager and if you don't constrain it, it will often take precedence.
One possibility is to define a controller and action for your custom route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("JsActionRoute", JsAction.JsActionRouteHandlerInstance.JsActionRoute);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
);
}
and then constrain the custom route by specifying a controller and action to be executed:
public static readonly Route JsActionRoute = new Route(
"JsAction",
new RouteValueDictionary(new { action = "JsAction", controller = "Foo" })
new JsAction.JsActionHandler()
);
Another possibility is to inverse the order of definition of your routes and constrain the default route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^((?!JsAction).)*$", action = "^((?!JsAction).)*$" }
);
routes.Add("JsActionRoute", JsAction.JsActionRouteHandlerInstance.JsActionRoute);
}