Html.ActionLink generates the wrong URL, aslways adding Terminal/ to the URL - asp.net-mvc

When I try to generate an HTML link using
#Html.ActionLink("Edit Carrier", "EditCarrier", "CustomerCare")
I would expect it to generate the URL /CustomerCare/EditCarrier/ but no matter what view I place it in the URL always gets generated as /Terminal/CustomerCare/EditCarrier/ and am I not sure why /Terminal/ is being added to the route. This is my first time NOT using Attribute Routing, and it is not an option to use it in this project. From looking around on the web I setup my RouteConfig.cs file as:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Terminals",
url: "Terminal/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
What am I missing here?

This is because the route Terminals matches first when you request the url. If you don't use this route, then you can simply remove it from RouteConfig.cs, then the url will be /CustomerCare/EditCarrier/. If you need the Terminals route for any controller, you can add constraints to it:
routes.MapRoute(
name: "Terminals",
url: "Terminal/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional },
constraints: new { controller = #"ControllerWhoNeedsThisRoute" }
);
EDIT: Alternative you could also use #Html.RouteLink() and add the route name for generating the url. But then you need to specify the controller and the action in parameters. The second parameter is the name of the route to use:
#Html.RouteLink("Edit Carrier", "Default", new { controller = "CustomerCare", action = "EditCarrier" })

Related

custom routing not working in MVC

I am creating a friendly URL so i have crete route in routeconfig file as follows
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ShowUser", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
and action link as follows
View
But it is not working giving runtime error "The resource cannot be found."
Default route and you custom route have identical structure, so default one will always be used because it is declared first.
Im guessing you are getting error because in code you are referring to ShowUser, but instead you should use name of the controller: UserGroupEdit.
View
If you want a friendly route you can do something like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ShowUser", // Route name
url: "ShowUser/index/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
}
in your cshtml
#Url.Action( "Index", "UserGroupEdit")
You can use UrlHelper.RouteUrl helper method which allows you to specify the route name:
View
RouteConfig:
routes.MapRoute(
name: "ShowUser",
url: "ShowUser/{action}/{id}",
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional }
);
Reference link

Why is this MVC route incorrect?

In my MVC application's RouteConfig I am using this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute (
name: "Dictionary", // Route name
url: "dictionary/{id}", // URL with parameters
defaults: new { controller = "Dictionary", action = "details" } // Defaults
);
At this time this URL succeeds in returning a page:
http://127.0.0.1:8080/dictionary/details/1
Whereas this fails:
http://127.0.0.1:8080/dictionary/1
With the exception
"HTTP 404.The resource you are looking for (or one of its dependencies) could have been removed, had its name changed..."
When I swap the routing around, like this:
routes.MapRoute (
name: "Dictionary", // Route name
url: "dictionary/{id}", // URL with parameters
defaults: new { controller = "Dictionary", action = "details" } // Defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It both URLs start returning a page.
Why is this?
Thanks in advance.
FYI This question is different to the possible duplicate because of the UrlParameter.Optional and the wild carded routing in the other question. This question is more about which specific ordering is correct and why.
The order of the routes that you add to the route table is important. If you reversed the order, then the Default route always will get called instead of the custom route.
Reference:
https://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/creating-custom-routes-cs

Change Index of Directory

I'm working on an ASP.NET MVC web application. By default, browsing to the root of a directory seems to call the controller's Index() method. Is there way to change which method is called by default here? I know I could probably name the method I want to call "Index" and it would likely work, but I'd like to know if there's a way to point the directory root to a method that I choose.
For example: mysite.com/MyDirectory/ will call Index(), which is effectively browsing to mysite.com/MyDirectory/Index. I'd like to change it so that mysite.com/MyDirectory/ calls Details, (or "browses" to mysite.com/MyDirectory/Details).
Just change the action in the default route. Something like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Details", id = UrlParameter.Optional } // Parameter defaults
);
You could specify the behavior in your routes. If you are using the latest version of MVC, that would be in your \App_Start\RouteConfig.cs
You would have something like:
routes.MapRoute(
name: "MyDirectory",
url: "MyDirectory",
defaults: new { controller = "MyDirectory", action = "Details" });
You would place this before your default route, as your route table acts kind of like a switch statement matching on the first route that it finds.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MyDirectory",
url: "MyDirectory",
defaults: new { controller = "MyDirectory", action = "Details" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

how to rewrite mvc routes to aspx

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

Route for XMLRPC Service breaks default route

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

Resources