Remove "Home/Index" secondary route to home page - asp.net-mvc

I have an ASP.net MVC 5 site. The home page is at http://mydomain.
However, there's also a second route to the home page - http://mydomain/home/index - which I think
This causes problems because it may be seen as duplicate content, and images are broken on this page.
How can I totally remove this route (so it goes to a 404, I guess?).
I've searched Google but can only find articles on removing Home from routes entirely - not what I need.
I'm using Attribute routing, and this is all that's in the RouteConfig.cs:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Enable Route Attributes in Controllers
routes.MapMvcAttributeRoutes();
// Fall through all routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The Home Index action has no attribute route on it (as you'd probably expect?). This /home/index route works even on newly generated MVC projects - which I think is a bad idea?
How can I do this?
Are there any problems with removing this route I may not have considered?
thx.

You can block unintended routes that you don't want by using IgnoreRoute().
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("Home");
routes.IgnoreRoute("Home/Index");
// Enable Route Attributes in Controllers
routes.MapMvcAttributeRoutes();
// Fall through all routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However, if these URLs are already in the wild, you should instead setup a 301 redirect to the canonical URL you intended. The simplest way to do that is with the URL rewrite module.
This /home/index route works even on newly generated MVC projects - which I think is a bad idea?
I see this as more of a blessing in disguise. It is an advantage over any SEO competitor using MVC who doesn't do the extra work to remove these routes when you are the one who does.

This is not necessary.
The default route provides optional controller and action names. So if user does not put any name for controller and/or action in path (/Home/Index or /Home in this situation) asp.net will put the right values in application routing.
Whenever you use Url.Action or Url.Route functions it will produce the shortest link for you. So in your website there will be always http://mydomain produced for your root. And for example Category > Index action it will produce http://mydomain/category.
In your website bots will never get to duplicate content if your links are in this way. If you are writing your links manually write as short as you can or simply use Url.Action.
About the images there must be something different, because images are static files. just use "~/imagefolder/imagename.jpg" way to get them. "~" is important to start link from the root of application if you are making your application work on a subfolder in IIS.

Related

Server-side routing with an AngularJS SPA

I'm using ASP.NET MVC, IIS 8 and I'm using AngularJS to create a SPA. I only have this routing rule defined in my MVC application, that directs to Index method in Home controller, where my SPA lives:
routes.MapRoute(
name: "DefaultHome",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
Now I have bunch of client-side SPA routes and I want all of them to available without the hashbang prefix #/. html5Mode works perfectly - but only when the app has been loaded. If I copy the URL, close the window, open the window and paste it in, I get an IIS 404 error. That makes sense because the routing is done on the server.
So sharing the URL doesn't work, which I would like to be able to do. So the routing has also to be done server-side, but everytime I change the route (add client side route), I don't want to have to create a server side routing rule.
Can this be done in general? Like something that URL rewrites /* to #/* without redirecting the user? With an exception for /Static.
Simply change the url to "{*catchall}". Like so:
routes.MapRoute(
name: "DefaultHome",
url: "{*catchall}",
defaults: new { controller ="Home", action= "Index" }
);

ASP.NET MVC Routes - /resourcename route

So, I would really like to create "landing page" routes for each of a particular type of entity.
So, let's suppose I have a site that is about comic heroes.
I would like landing pages like http://myherosite.com/superman and http://myherosite.com/batman, etc.
I know how to accomplish this with something like http://myherosite.com/heroes/superman and http://mysite.com/heroes/batman. The "heroes" in the URL allow for a specific route and thus controller and default action.
Is it possible to setup a route that will accomplish this and still leave the default route ("{controller}/{action}/{id}") in place (I am using that).
Thanks
You could add a route before "Default" that interprets all single-segment paths as containing an action method of the heroes controller.
routes.MapRoute(
name: "Landings",
url: "{action}",
defaults: new { controller = "Heroes" }
);
Of course, that would prevent you from using any default action for the default route because those are single segments paths as well.

Why doesn't Default route work using Html.ActionLink in this case?

I have a rather peculiar issue with routing.
Coming back to routing after not having to worry about configuration for it for a year, I am using the default route and ignore route for resources:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
I have a RulesController with an action for Index and Lorem and a Index.aspx, Lorem.aspx in Views > Rules directory.
I have an ActionLink aimed at Rules/Index on the maseter page:
<li><div><%: Html.ActionLink("linkText", "Index", "Rules")%></div></li>
The link is being rendered as http://localhost:12345/Rules/ and am getting a 404.
When I type Index into the URL the application routes it to the action.
When I change the default route action from "Index" to "Lorem", the action link is being rendered as http://localhost:12345/Rules/Index adding the Index as it's no longer on the default route and the application routes to the Index action correctly.
I have used Phil Haack's Routing Debugger, but entering the url http://localhost:12345/Rules/ is causing a 404 using that too.
I think I've covered all of the rookie mistakes, relevant SO questions and basic RTFMs.
I'm assuming that "Rules" isn't any sort of reserved word in routing.
Other than updating the Routes and debuugging them, what can I look at?
Make sure there is not a folder called 'Rules' in the same directory as your website. In its default configuration, ASP.NET MVC routes will respect physical paths before route definitions. If there is a route defined which matches the path to a physical folder in the website, the routing engine will be bypassed completely.
You can disable routing to physical paths by changing the RouteTable.Routes.RouteExistingFiles property to false, but if you do this and your application has paths to physical resources (such as images, scripts, stylesheets, etc) you will need to accommodate for those paths with matching IgnoreRoute() definitions.
For example: RouteTable.Routes.IgnoreRoute("content/{*pathInfo}");.

Default route problem

If I want to hit a url like
http://localhost:8080/controllername
I want the "Index" action to be the default action called. I assumed the default route mapping would be fine and the "Index" action would be called on whatever controller was specified - seems I need to specify
http://localhost:8080/controllername/index
Is this correct?
Mapping:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
What you're trying should definitely work. In fact, the code you posted is from the default templates, and I've just tested it by adding an "Index" action to the AccountController and visiting /Account in my browser.
I'd recommend creating a new project and testing this behaviour (first with the built-in web server, then with IIS, if you're not always using the built-in server). If it works, there's probably something different in your project that's causing the issue.
I had a similar problem and it occured because of a collision with a directory in the project. I had a structure like this in my project:
Controllers \
HomeController.cs
CmsController.cs
Cms \
WhateverFile.cs
The Cms subdirectory collided with the /Cms URL, while Cms/Index worked. I simply renamed my colliding folder name. If you have to keep it, there is a RouteCollection.RouteExistingFiles that can be used to prevent automatic lookup of files. If that is enabled I think that a lot exclusions have to be added for the Script etc, see this blog post for an example.

Strange route problem in ASP.NET MVC - default route not hit

I have this two routes currently in my application after decommenting out many other ones. Let me first explain that I have quite a big application already but have come to a problem where my application does not start at the root url anymore.
If I set starting page to default.aspx then webapp starts at (example) http://localhost:55421/Default.aspx. I don't want that. I want it without Default.aspx
So I went into app properties and removed Default.aspx as starting page - now it is blank field (just like in a sample new MVC app if you create it in VS 2008).
But now application does start at the required URL but issues an error:
"The incoming request does not match any route."
Also If I use route debugger it also misses all routes and catches it by catchall route.
I don't know how all of this is possible since as I said above I have two default routes configured at this time:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pages", action = "Display", slug = "Default" }
);
Any help appreciated
Am I right in thinking you are trying to hit
http://server/{controller}/{action}/{id}
with
http://server/
If you are I think you need to provide a default for the last parameter {id}. You have a default for a parameter slug but without a default for {id} I don't think ASP.NET Routing can hit it.
If I'm right
http://server/Pages/Display
should also not hit the default route, because you are expecting id in Display?
HTH
Alex

Resources