Remove action from url in asp.net mvc razor c# - 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

Related

Is it possible to have more than one routing in ASP.NET MVC?

I want to ask if it's possible to have more than one routing within RouteConfig class. The logic I have is as below, what I want to achieve. I have actionLink("Dashboard", "Account".....) already and want to have a unique one that won't conflict with existing one when page is loaded. Please assist me there is a way.
namespace ContentManagementSystem
{
public class RouteConfig
{
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 already have this working fine
defaults: new { controller = "Dashbaord", action = "_Index", id = UrlParameter.Optional // I want to have separate, but unique route for this controller for actionResult
);
}
}
}
Yes you can have more routes you can :
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Student",
url: "students/{id}",
defaults: new { controller = "Student", action = "Index"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
As shown in the above code, URL pattern for the student route is students/{id}, which specifies that any URL that starts with domainName/students, must be handled by the StudentController.
Notice that we haven't specified {action} in the URL pattern because we want every URL that starts with student should always use Index action of StudentController. We have specified default controller and action to handle any URL request which starts from domainname/students.
You can read more here
Add it to top of default route:
namespace ContentManagementSystem
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("other_route", "other_route/",
defaults: new { controller = "OtherController", action = "OtherAction" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // I already have this working fine
defaults: new { controller = "Dashbaord", action = "_Index", id = UrlParameter.Optional // I want to have seperate, but unique route for this controller for actionResult
);
}
}
}

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?

Issue related to routing in MVC

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 }
);
}

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?

MVC4 controller/route not working

my VisitsController:
public ActionResult Index(Visits visits, int? id)
{
....
return View(v);
}
Here is my route config
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 }
);
}
Works: http://localhost:49225/Visits
Does not work: http://localhost:49225/Visits/123
What would the custom route be (and force it to be an integer)?
You need to add a new route for that to work.
Currently, this will work:
/Visits/Index/123
I believe you need to add the following:
routes.MapRoute(
name: "VisitsDefault",
url: "Visits/{id}",
defaults: new { controller = "Visits", action = "Index",
id = UrlParameter.Optional }
);
This is assuming you have a modelbinder for Visits already.

Categories

Resources