How can I make URL routing for subfolders in MVC? - asp.net-mvc

I am new to the Asp.net MVC4 , I need to define rout for folder structure mention below,
Views/Home (Desired URL: /Home)
Views/Admin/CorePages/City (Desired URL: /Admin/CorePages/City)
for 'Home' URL routing working perfect but for sub-folder such as 'Admin/CorePages/City' it is not routing as per URL,How do I create route?
Note: My Controller are place in same manner in controller folder.

Related

How are the default MVC Controllers added to the route table

We says that the MVC request is processed by the controller/action which are mapped in the Routetable.
In routeconfig file, there is only one (default) route is mapped. But how all the controller/action excecuted correctly. How the other routes are added in the routetable ?
The default route works by convention, which looks for classes in the Controllers folder that end with the word controller (i.e. OrdersController). In this case the route will automatically be added to your routing table.
You can find the full documentation for ASP.Net Routing here: https://msdn.microsoft.com/en-us/library/cc668201.aspx
You need to do some reading here to fully understand how ASP.NET routing works.
https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

Default/Index route clashing with an existing folder causing 403

I have a legacy page that I've just converted to MVC - from "/content/Default.aspx" to "/content/Index". The content folder also contains css files etc.
IIS is attempting to list the folder contents when the default route is requested and returns a 403 response code.
Is there a non-hacky way to get around this. Right now, I'm rewriting the URL in the .htaccess file to use the 'Index' view but would prefer not to do this.
Here's the route:
routes.MapRoute(
"Content",
"content/{action}",
new { controller = "Content", action = "Index" }
);
Here's the hack that I'm currently using:
RewriteRule ^/content/(\?.*)?$ /content/Index$1 [NC,L]

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!

Outgoing Routing in asp.net mvc

I'm reading Pro ASP.NET MVC 3.0 from Appress.Chapter 11 is about URLs routing system.
In passing extra variables from outgoing section is explained about getting segment values from url
for example for bellowing routing :
routes.MapRoute("MyRoute", "{controller}/{action}/{color}/{page}");
if a user is currently at the URL /Catalog/List/Purple/123, and we render a link as
follows:
#Html.ActionLink("Click me", "List", "Catalog", new {page=789}, null)
The routing system will match against the route and It will generate the
following HTML:
Click me
But when i use this code for the following example it generates:
Click me
i don't understand why?
Try moving that route mapping above the other route mappings in your global.asax file

Running ASP.NET MVC in a subdomain makes Html.ActionLink render broken links

I am running MVC in a subdomain
http://test.domain.com which points to the /Test directory on my webhost4life account.
Html.ActionLink("About", "About", "Home")
it renders a link to
http://test.domain.com/Test/Home/About -- which gives a 404
the link should be ..
http://test.domain.com/Home/About
is there a way to override ActionLink to omit the /Test on render?
Thank you
Experiment 1
I added a route to the table like this...
routes.MapRoute(
"Test", // Route name
"Test/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
and now action link renders links like this..
http://test.domain.com/Test/Test/Home/About/
when this is clicked it does not give a 404 but gives the Home controler About action.
Result
No more broken links but the site renders ugly urls.
For a site using lots of subdomains I use a nifty MVC extension from ITCloud called UrlRouteAttribute. It allows you to assign a route to every action as an attribute setting the path and name. I have extended this to allow fully qualified paths - so to include the domain/subdomain the controller should attach to. If this is something you'd be interested in I'll upload a copy somewhere.

Resources