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") }
);
Related
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
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" })
I am using mvc4 framework and .net framwork 4.5. I need a url like this:
www.examples.com/name (note: 'name' will be change dynamically)
which routes to the same page.
I have tried like this but getting error
My action method is like this:
public ActionResult Userlist(string status)
{
return View();
}
Route Config
routes.MapRoute(
"user",
"{status}",
new { controller = "Home", action = "Userlist" }
);
How can I create a route syntax and redirect this to a controller?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Add this route config
routes.MapRoute(
name: "Default_Userlist",
url: "{status}",
defaults: new { controller = "Home", action = "Userlist" },
namespaces: new[] { "MyMvcProject.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyMvcProject.Controllers" }
);
}
Don't forget to replace the "MyMvcProject" with the name of your project.
Issue
This is a bad practice because the value of the status could easily cause conflicts with your action methods.
Hi I have a URL as follows in my MVC 4 application
http://localhost/ABC/Home/DeleteApp/3000
and I have configured RouteConfig.cs as follows
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It goes to the Correct DeleteApp Controller but with the ID value Null. It shows the correct URL when I enable firebug.
What could possibly go wrong?
it debugs and come to here but the id field as null;
public void DeleteApp(string id)
{
try
{
// delete logic
}
catch (Exception e)
{
//
}
}
Possibles causes :
-> Check your "id" variable name (must be exactly the same name in the RouteConfig and in the controller action, it is case sensitive)
-> You have other routes mapped before this one that match the pattern
If you have a route mapped as {controller}/{action}/{something} and another route mapped as {controller}/{action}/{something_else}, when you call the url "Home/DeleteApp/3000", it can't tell if 3000 is "something" or "something_else" so it will take the first match.
To make this working, you have to use "Route Constraints"
http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC
You need to do it like so:
routes.MapRoute(
name: "ABC",
url: "ABC/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you want to have url in this format http://localhost/ABC then you will have to modify the route as below by adding ABC to {controller}/{action}/{id} expression.
routes.MapRoute(
name: "Default",
url: "ABC/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
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