ASP.NET MVC Routes - /resourcename route - asp.net-mvc

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.

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.

Is there a simple way to map a single URL to another URL in ASP.NET MVC?

I have a working ASP.NET MVC application to which I would like to add a very simple URL which I would like to be redirected to another URL, without the Controller part of the path in it.
I.e. I want to be able to tell people to go to mySite.org/Hello and have it direct them to mySite.org/Whatever/WhateverElse?myfun=9, or whatever.
So I added a method in PublicController that redirects Hello to whatever, which works, except it requires me to tell people to go to mySite.org/PUBLIC/Hello, and we'd like to not have the extra word "public" in there.
Hours of confusing study later, I see that I could perhaps add a line in Global.asax.cs such as: routes.MapRoute(name: "Hello", url: "Public/Whatever"); ... or something... but that actually changes the way everything gets routed, and after trying various syntactical variations, nothing has seemed to just change one basic address to map to another basic address.
Is there a simple way to get mySite.org/Hello to map to another URL, without the virtual subdirectory for the public controller?
Create a custom route (it would need to be the first route in the table)
routes.MapRoute(
name: "Hello",
url: "Hello",
defaults: new { controller = "Whatever", action = "WhateverElse" }
);
then /Hello will redirect to /Whatever/WhateverElse

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!

Github like routes

How is this achievable in asp.net mvc? I have seen some ruby examples out there...
Github like routes in Rails
i.e. nested folders, can be unlimited... however they are usually nested once
e.g.
lets say my main route in my app is:
/projects/{projectid}/
I am hooked into a file system, so I want users to navigate through whatever directory structure so I can have:
/projects/{projectid}/foldera/
/projects/{projectid}/folderb/
/projects/{projectid}/foldera/pic1.png
/projects/{projectid}/folderb/special/car134d.jpeg
etc...
This way I can only show files or pictures that are inside the url/directory the user is in...
I think what you're after is the {*queryvalues} segmented url handler for routing. The * indicates a wildcard-style match of one or more segments, delimited by a "/".
To map to a physical path, you can use this to add a route to your Global.asax.cs:
routes.MapPageRoute(
"Projects", // Route name
"projects/{projectId}/{*path}", // Route url format
"~/ProjectFolder/{projectId}/{path}" // Path to files
);
If you want to get the path into an MVC Controller Action and then do something with it, you could use MapRoute:
routes.MapRoute(
"Projects", // Route name
"projects/{projectId}/{*path}", // Route url format
new { controller = "Project", action = "Index" } // Defaults
);
Then create a ProjectController, and in the Index action retrieve RouteData.Values["projectId"] and RouteData.Values["path"] and do whatever you need to with them...

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}");.

Resources