Default route problem - asp.net-mvc

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.

Related

Remove "Home/Index" secondary route to home page

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.

Changing URL without changing Actual Path to Redirect

I am new to ASP.Net and working on MVC 4. I want to replace my current URL with a customized URL.
For example:
Current URL: http://www.testsite.com/home?pageId=1002
Desired URL: http://www.testsite.com/1002/home/
So the URL that is displayed in the address bar will be the desired one and actual URL working will be the current one.
I have tried URL routing in Global.asax file of my project but doesn't seems to be working for me.
What exactly I want is to put the URL Like this.
Thanks in Advance.
ASP.NET MVC 4 provide a toolbox way to write your application. The URL that you see in the browser comes from Routing that do the hard work to convert url to app routes and app routes to url.
1) The default ASP.NET MVC 4 Template project comes with a file at App_Start folder named RouteConfig, where you must config the routes for the app.
2) The routes has precedence order, so, put this route before the default one:
routes.MapRoute(
name: "RouteForPageId",
url: "{pageId}/{action}",
//controller = "Home" and action = "Index" are the default value,
//change for the Controller and action that you have
//pageId is the parameter from the action that will return the page
defaults: new { controller = "Home", action = "Index" }
);
Now you can enter myappdomain/1220/index for exemple.
Hopes this help you! Take a look here for more info ASP.NET Routing!

I'm getting a "Does not implement IController" error on images and robots.txt in MVC2

I'm getting a strange error on my webserver for seemingly every file but the .aspx files.
Here is an example. Just replace '/robots.txt' with any .jpg name or .gif or whatever and you'll get the idea:
The controller for path '/robots.txt'
was not found or does not implement
IController.
I'm sure it's something to do with how I've setup routing but I'm not sure what exactly I need to do about it.
Also, this is a mixed MVC and WebForms site, if that makes a difference.
You can ignore robots.txt and all the aspx pages in your routing.
routes.IgnoreRoute("{*allaspx}", new {allaspx=#".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*robotstxt}", new {robotstxt=#"(.*/)?robots.txt(/.*)?"});
You might want to ignore the favicon too.
routes.IgnoreRoute("{*favicon}", new {favicon=#"(.*/)?favicon.ico(/.*)?"});
You can adjust the regular expression to exclude paths.
Haacked from the source.
The ignore route given above didn't work for me but I found a similar one that did:
routes.IgnoreRoute("{*staticfile}", new { staticfile = #".*\.(css|js|gif|jpg)(/.*)?" });
This error could also happen if inside a view in your area, you use the Html.Action helper. This helper will always use the area as a prepend, unless you specifically tell it not to. E.g.,
#Html.Action("Main", "Navigation", new { area = string.Empty })
I found another solution too... While I don't think I'll use it, it's worth showing here in the answers:
The following should (in theory) ignore looking for controllers for anything with a '.' in it.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = #"[^\.]*" } // Parameter contraints.
);
Do you still have:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
... in your Global.asax.cs?
MVC puts it there by default, and it's supposed to handle this.
If you do, then the problem may be how you're mixing MVC and WebForms.
I encountered this error when I request resources that did not exist.
Specifically, I was requesting a custom IE css file:
<!--[if lt IE 8]>#Styles.Render("~/Content/ie7.css")<![endif]-->
(These are condition comments, interpreted by IE)
However, the actual resource existed on ~/Content/ie/ie7.css.
So, without any modifications to the routing, the error was solved by using the correct url of the resource.

ASP.NET MVC - Running in a Subdirectory

I am attempting to deploy an ASP.NET MVC application in a subdirectory of an existing application and I am running into some routing issues. I have set up the folder structure such that all of the binaries and config files for the MVC app are correctly located in the root directory, while the rest of the content is in the subdirectory. Additionally, I updated all of the routes in the MVC application to reflect the subdirectory; however, every request to the application produces:
The incoming request does not match
any route.
All defined routes are being ignored, including the default route:
routes.MapRouteLowercase(
"Main_Default",
"blog/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
I tried enabling RouteDebug to test the issue, but even that is not getting routed to. Any advice on what else I can try?
Note: This question is not a duplicate.
Try running it as a virtual directory instead of just a directory, otherwise your routes are not going to be called. You will not need to put the name of the virtual directory in the route.
Here's a route that I have setup in a v-dir MVC app that works just fine...
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Tour", action = "Index", id = "" } // Parameter defaults
);
Looks like I found the problem.
In addition to the binaries and the config files, Global.asax must also be placed in the root in order for its code to be executed.
Thanks guys. :)

MVC Catch All route not working

My first route:
// Should work for /Admin, /Admin/Index, /Admin/listArticles
routes.MapRoute(
"Admin", // Route name
"Admin/{action}", // URL with parameters
new { controller = "Admin", action = "Index" } // Parameter defaults
);
is not resolving the route(I use Phil Haack's Route Debugger) and even the last route, "Catch All" route does not work:
//Maps any completely invalid routes to ErrorController.NotFound
routes.MapRoute("Catch All", "{*path}",
new { controller = "Error", action = "NotFound" }
);
If I go to /Admin/listArticles it works but /Admin gives me Error 403.15 "The Web server is configured to not list the contents of this directory." That points me to the idea that no routing is used as it looks for a physical file in a directory?
This is a simple low-level route problem but I cannot get it to work and everybody gives me links to read (yes I know MSDN is out there) but no real answers. I have researched routes and have tried but I am posting this because I cannot get it to work, any help, answers?
The answer to my question was that I had a route called /Admin and I wrote my error log to a directory /Admin/Error It seems that there is no overload to specify if the route should be resolved or if it is part of a physical directory.
The problem might be that you have added this route below the default route, all custom routes should be added above default route.
Are you using IIS 6.0? If so it'll need to look like...
// Should work for /Admin, /Admin/Index, /Admin/listArticles
routes.MapRoute(
"Admin", // Route name
"Admin.mvc/{action}", // URL with parameters
new { controller = "Admin", action = "Index" } // Parameter defaults
);
Where you need to set mvc as an application extension

Resources