Issue related to routing in MVC - c#

I have MVC webapplication. i have written following rules in Route.config file
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.MapRoute(
name: "about",
url: "Home/About-WDI",
defaults: new { controller = "Home", action = "About"}
);
}
and in global.asax file, i have written following code:
RouteConfig.RegisterRoutes(RouteTable.Routes);
now problem is when i type http://localhost:60123/Home/About-WDI
it gives me error saying Resources not found
what is issue in above code?
Thanks

As already mentioned in the comments, the order of the routes is taken into concern. And as your {id} parameter is optional the URL will be matched by the first route. So simply exchange both routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "about",
url: "Home/About-WDI",
defaults: new { controller = "Home", action = "About"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

Related

Hiding Action name from URL

My url is this,
localhost:45678/Blogs/Index
I need it like this format
localhost:45678/Blogs
i write below code in route file but,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Blog",
url: "{Blog}",
defaults: new { controller = "Blog", action = "Index" }
);
routes.MapRoute(
name: "forblogcontent",
url: "Blog/{slug}/{id}",
defaults: new
{
controller = "Blog",
action = "DisplayContent",
slug = UrlParameter.Optional
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Content", action = "AddContent", id = UrlParameter.Optional }
);
}
When i click on an link button it still shows
localhost:45678/Blogs/Index
My link button code is
Blogs
How to correct this?

Remove action from url in asp.net mvc razor c#

I have a url like this:
http://localhost:57482/ModuleName/Index/Value
I want to change it like this: http://localhost:57482/ModuleName/Value
Code in RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{Category}",
defaults: new { controller = "Product", action = "Index", Category = UrlParameter.Optional }
);
}
Am I doing anything wrong?
You are specifying your URL as being of the format {controller}/{action}/{Category}, and in the next line you say action = "Index". So, instead of:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{Category}",
defaults: new { controller = "Product", action = "Index", Category = UrlParameter.Optional }
);
}
try:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{Category}",
defaults: new { controller = "Product", action = "", Category = UrlParameter.Optional }
);
}
Not sure if you need the action = "" part, so you might as well try omitting it

Retrieve QueryString And Force Custom Url To A Page

I am trying to config custom url in the RouteConfig.cs file as follows with the default one:
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.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
}
The above works fine when I write the followings in the url: 1st routing
http://localhost:1234/Product/AddImages/id/1008
Again, the below also works: 2nd routing
http://localhost:1234/Product/AddImages/1008
But I want to force the url to show or write the url in the address bar as the first one for the specific page 'AddImages' like this: http://localhost:1234/Product/AddImages/id/1008
Is there any way to do it I mean keeping the default routing and for a specific page, the first routing option?
Again, I tried to retrieve the QueryString from the first url as follows:
int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //This works for the second routing option
It says - Input string wasn't in a correct format. I guess, this is for the default routing option as it's configured to:
url: "{controller}/{action}/{id}"
Again, is it possible to get the QueryString value from the below url or to config anything:
http://localhost:1234/Product/AddImages/id/1008
In routing table for MVC, the route that are defined first has the highest priority. So all you need is make sure your specific route higher priority than the default one. Just define it earlier. That is it - as mentioned in the comment.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
If you want only the first one then try removing the generic one
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
}
If you want it only for that controller only try this -
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "Product/{action}/id/{id}", //Custom url route
defaults: new { action = "AddImages", id = UrlParameter.Optional },
constraints: new { controller = "Product" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
There is a better way to get url parameters. Instead of
int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //
try this -
int id = Convert.ToInt32(Request.Params["id"]); //
But if you defined your action method properly, you don't even need to read it from request. It will automatically be mapped.

Unable to Configure Routes Properly in my ASP.NET MVC app

I am configuring my routes in asp.net mvc app where there are multiple routes and only first and last route works but the routes defined in the center of my route.config file are not working. Following is the code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Doctor",
url: "admin/doctor/{action}/{id}",
defaults: new { controller = "admin", action = "AddDoctor", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Admin",
url: "admin/{action}/{id}",
defaults: new { controller = "admin", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
It seems that you forgot the controller part of your Admin route.
You should define it as below:
routes.MapRoute(
name: "Admin",
url: "admin/{controller}/{action}/{id}",
defaults: new { controller = "admin", action = "Login", id = UrlParameter.Optional }
);

Mvc 5 supporting legacy ashx links

Is there a way to support ashx links as in MapRoute?
For example I have this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ashx",
url: "test/redirect.ashx",
defaults: new { controller = "Click", action = "Redirect" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This doesnt work but what im looking for is, if the url starts is www.example.com/test/redirect.ashx?foo=bar it will redirect me to my controller ClickController.Redirect(). is this possible? Am i Doing this wrong?

Categories

Resources