asp.net mvc maproute - asp.net

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..

Related

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 Route - example.com/blog/projects to example.com/projects

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.

HTML.ActionLink() generating incorrect URLs

I'm having difficulty getting HTML.ActionLink() create correct links.
When I am at
http://localhost:49242/
My options are:
http://localhost:49242/en-us/PinnedSites ,
http://localhost:49242/en-us/addons ,
http://localhost:49242/en-us/trackingprotectionlists
But when I navigate to http://localhost:49242/en-us/PinnedSites my options are:
http://localhost:49242/en-us/PinnedSites/PinnedSites ,
http://localhost:49242/en-us/PinnedSites/addons ,
http://localhost:49242/en-us/PinnedSites/trackingprotectionlists
All of these urls fail, of course.
Here's my html:
.cshtml (Html / razor)
<li id="nav_pinning">
#Html.ActionLink( "pinned sites", "index", "PinnedSites")</li>
<li id="nav_addons">
#Html.ActionLink("add-ons", "index", "addons")</li>
<li id="nav_control">
#Html.ActionLink("tracking protection lists",
"index",
"trackingprotectionlists",
null,
null)</li>
Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{culture}/{controller}/{action}",
new { culture = "en-us",
controller = "Home",
action = "Index",
}
);
}
Where is my problem? Is it possible that /PinnedSites/ is not actually going to the PinnedSitesController?
(And I will be actively watching this, please comment if you want me to try things or provide more code.)
Do you have a HomeController as part of your project? If so you should register the default controller along the lines of:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
However, that aside have you tried simply making the Route directly to your PinnedSites controller?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{culture}/{controller}/{action}",
new { culture = "en-us",
controller = "PinnedSites",
action = "Index",
}
);
}

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.

Resources