ASP.NET MVC3 Route - example.com/blog/projects to example.com/projects - asp.net

I have an ActionResult in my Blog controller that is returning all the projects called Projects, but my URL looks like example.com/blog/projects, I want to set up route to make it look like example.com/projects.
Is it possible? And if it is then how can I achieve that?

You have to register route following way Global.asax and put this as a first route
routes.MapRoute("Projects", "Projects" , new { controller="blog" , action="Projects" });

You can add a route to your Global.asax.cs file, such as:
routes.MapRoute(
"Projects", // Route name
"Projects/{id}", // URL with parameters
new { controller = "Blog", action = "Projects", id = UrlParameter.Optional } // Parameter defaults
);
So it looks something like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Projects", // Route name
"Projects/{id}", // URL with parameters
new { controller = "Blog", action = "Projects", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Make sure you put your new route above the default one otherwise yours will never be hit.

Related

RedirectToAction() generates wrong URL

I'm having a problem with MVC 4, and I guess it's something really trivial, but it's been bugging me for the last day and I can't seem to figure it out.
I have this url:
http://www.example.com/my-dashed-url
I have a Controller named:
public class MyDashedUrlController: Controller
{
}
I have only two Routes like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("my-dashed-url",
"my-dashed-url/{action}",
new { controller = "MyDashedUrl", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
I get to the index just fine. However, when I do this:
public ActionResult Index()
{
if (NoUserIsLoggedOn)
return RedirectToAction("Logon", "MyDashedUrl");
return View();
}
public ActionResult Logon()
{
Contact c = GetContact();
return View(c);
}
It doesn't redirect me to the "Logon" action properly.
It should redirect me to:
http://www.example.com/my-dashed-url/logon
but instead it tries to redirect me to:
http://www.example.com/logon
... which doesn't work (404 Not Found)
I'm missing something. Can anyone spot it? If anyone needs any more information, let me know.
And it's EVERY RedirectToAction that does the same thing in this controller. A Html.BeginForm("Logon", "MyDashedUrl") would also generate:
http://www.example.com/logon
I guess it has to do something with the Routes I defined, but I can't find the faulty one, seeing as they're all the same. If I disable all of my Routes besides the default one from MVC, the problem remains the same
Make sure that you have declared this custom route BEFORE the default one:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"my-dashed-url",
"my-dashed-url/{action}",
new { controller = "MyDashedUrl", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Remember that routes are evaluated in the order you declared them. So the first route that matches a request will be used. If you declare your custom route after the default one, it is the default route that will match the request.

Url.Action does not use desired route for output

I have the following route definitions in my MVC3 website:
routes.MapRoute(
"FB", // Route name
"fb/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
).RouteHandler = new RH();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
My custom "RH" handler's code is
public class RH : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
//here I store somewhere that 'fb' prefix is used, so logic is different in some places
return base.GetHttpHandler(requestContext);
}
}
What I want to achieve, that when my website is accessed with the 'fb' subpath-prefix, then my website-logic executes a little bit different way.
The problem is, that when I access my site normally (eg. http://localhost), then when I execute
Url.Action('action' 'controller')
, then the output is "http://localhost/fb/controller/action".
I want to achieve, that when my site was accessed with 'fb' prefixed subpath, then my Url.Action calls output /fb/controller/action path, and if I access the website normally (without 'fb' prefix subpath), then Url.Action calls output /controller/action
The main thing, that /fb/controller/actions have to route to the same controllers/actions as when the site is accessed via /controller/action format.
The 'fb' route is just needed to store some temporary info when 'fb' prefix i used.
Seems I found a solution based on this link (MVC 3 Routing and Action Links not following expected contextual Route), new path-placeholder introduced and constraint added.
Maybe it's not good enough, or you know better than this, but seems to work for me:
routes.MapRoute(
"FB", // Route name
"{path}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new { path = "fb" }
).RouteHandler = new RH();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

asp.net MVC3 global route and hardcoded routes

I have an application that I am using a global route to query for the current path and return page specific data. I have the routes setup like this...
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Pages",
"Pages",
new { controller = "Pages", action = "Index" });
routes.MapRoute(
"Navigation",
"Navigation",
new {controller = "Navigation", action = "Index"});
routes.MapRoute(
"Default", // Route name
"{*url}", // URL with parameters {controller}/{action}/{id}
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The problem I am facing is when I go to /Pages to try and add new pages, the PageController fires like it is supposed to, but when debugging, after going to /Pages the app then makes a request for the HomeController. Am I missing something in my routing setup?
The Default route is firing because of the {*url}. So any page that's not /Pages, will go to the default route.
I need more info, but if you're trying to do /Pages/whatever, then you need to add an optional parameter on your Pages route:
routes.MapRoute(
"Pages",
"Pages/{page}",
new { controller = "Pages", action = "Index", page = UrlParameter.Optional });
Your default route is incorrect. It should look like the default route as defined when you open a new MVC 3 project, like so:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The problem is that the default route you defined will not parse any requests which reach it.

Routing table is not working

HI I have the following routing table
routes.MapRoute(null,
"Save", // Route name
new { controller = "Package", action = "Save"} // Parameter defaults
);
routes.MapRoute(
"Package", // Route name
"{controller}/{action}/{name}/{p}", // URL with parameters
new { controller = "Package", action = "Index", name = UrlParameter.Optional, p = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
When i am typing the path /Package/Save it is showing me page not found.
Can anybody please tell me what i am doing wrong?
THanks
The first route is configured incorrectly - name of route, then the pattern and then the defaults.
Also make sure you have a Package controller and a Save action method on it.

asp.net mvc maproute

Greetings,
I have a problem with link in mvc application. When I run it via Visual Studio it's ok. The link then is as follows:
http://localhost:2566/ActivateClient/Activate/6543e2d6-707d-44ae-94eb-a75d27ea0d07
when I run it via IIS7 the link is as follows:
http://localhost/ActivationService/ActivateClient/Activate/6543e2d6-707d-44ae-94eb-a75d27ea0d07
The default route is as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
I assume I have to change this MapRoute, am I right? How to change it? ActivationService is my virtualDirectory in IIS. Can someone help me with this please?
I also tried to maproute as follows:
routes.MapRoute(
"Default", // Route name
"ActivationService/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
but also without success
Did you add the new one or replace the existing one?
If you added, you need to position it before the existing one.
routes.MapRoute(
"Default", // Route name
"ActivationService/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The rules take precedence..

Resources