MVC Routing Inside a Folder - c#

I have built a standard MVC application with controllers and views and the following routes:-
routes.MapRoute
(
name: "PageNumber",
url: "{controller}/{action}/page-{pageNumber}",
defaults: new { controller = "Home", action = "PageNumber" }
);
routes.MapRoute
(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
Because this a back-end system, there will be a basic HTML website on the front of this. It means I need to route my site into a subfolder, so that the URL's look like this:-
SubFolder/Controller/Action/{id}
How can I do this, without changing all of my hard-coded links to include this sub-folder. I can't use MVC Areas for this, so was wondering if there was a way of changing the routing to automatically pre-pend the SubFolder bit of the URL?
Thanks!

You could make a new route:
routes.MapRoute(
name: "Subfolder",
url: "Subfolder/{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
However you should not be hard coding links, if you have future plans to replace the basic HTML page you could use #Html.ActionLink to generate the anchor tags for you.

Related

ASP.Net C# MVC create exception to a global route

I am writing a URL Shortener application.
When someone enters root.com/whatever, they are redirected to a configured URL.
I managed to create a global route which will catch the paths after the root ("whatever" above) and execute the corresponding redirection successfully.
My problem and question is this:
The admin interface is at root.com/admin and when I try to access that, I get the global controller. How do I make an exception to the global controller for "admin"?
Here is what I have in my route config right now:
routes.MapRoute(
name: "Admin",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Global",
url: "{suffix}",
defaults: new { controller = "Home", action = "Index", suffix = UrlParameter.Optional }
);
For the first route, I also tried:
routes.MapRoute(
name: "Admin",
url: "admin/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
And I also tried putting it second in the file.
I don't know if this will help you, but take a look at this stack overflow posting: ASP.Net MVC: Routing issue which throwing exception
This person reduced the more specific route and it worked for him.

MVC routes are in conflict

I need to have 2 routes in my ASP.NET MVC application, and I am having difficulty trying to define the routes in a way that they don't steal from each other.
I need to handle urls like
www.website.com/Home/About
www.website.com/Home/Contact
I also need to urls like this
www.website.com/23a244x3/Products/2
www.website.com/434e5s344/Services/1
www.website.com/f2432g423/Profile
So I need 2 routes.
The urls in #1 are successfully handled by the default route, which is
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
To handle the urls in #2, I have defined the following route
routes.MapRoute(
name: "PagesRoute",
url: "{uniqueID}/{action}/{pagecount}",
defaults: new { controller = "Pages", pagecount = UrlParameter.Optional }
);
Unfortunately now the route #2 (aka PagesRoute) is also handling the urls from #1, such as www.website.com/Home/About. I also have url rewrite rules set in IIS that turn all urls into lowercase, for SEO purposes, so /Home redirects to /home etc.
How can I differentiate these two routes? The route in #2 seems to be greedy..
You would have to not provide controller, and provide routes for each controller:
routes.MapRoute(
name: "Default",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
To the parser {uniqueID} and {controller} are essentially the same. Any string in the Url, will match both routes, so you need something to differentiate the 2.
You can also take a look into Route Constraints:
routes.MapRoute(
"PagesRoute",
"{uniqueID}/{action}/{pagecount}",
new { controller = "Pages", pagecount = UrlParameter.Optional },
new { uniqueID = #"\d+" }
);

ASP MVC Simple Routing Optional Parameter

I have a couple of routes in my config that seem to work OK in the IIS express dev environment, but not in IIS. I can get to the route BlogArchive route, but not the BlogDetailroute.
routes.MapRoute(
name: "BlogDetail",
url: "Blog/{Slug}",
defaults: new { controller = "Blog", action = "Detail", slug = UrlParameter.Optional }
);
routes.MapRoute(
name: "BlogArchive",
url: "Blog",
defaults: new { controller = "Blog", action = "Index" }
);
This can be fixed by just adding an additional pointer to the BlogDetail i.e. making the whole url map to Blog/Entry/{Slug}, but I would rather not have to have that extra bit. I can't seem to find any examples on the web, so excuse me if I am missing something simple.

Rewriting url in asp.net-mvc4 (razor) with extention

Default of asp.net-mvc4 is http://domainname.com/products/1 with routes
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
and I want to rewrite to http://domainname.com/products/1.html that has .html extention .
Any ideas for this?
Do you mean this:
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}.html",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
Why would you want to put .html extension to a query string or route parameter?
if you take a website with html files, the extension is with the page not with the parameters. Don't know if you have any specific requirement to put it always at the end of the url. but it doesn't make any sense.
routes.MapRoute(
name: "Products",
url: "{controller}/{action}.html/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
you might have problems with iis after doing the above, please take a look at this discussion as well.
ASP.NET MVC Routing - add .html extension to routes

root directory of MVC not found

I'm new to MVC4 (or MVC at all using .NET). I have my controller and views working fine but if I navigate to
http://localhost:<port>
I get a 404. If I go to "
http://localhost:<port>/MyController
everything works fine. How do I get a default controller for ROOT of the website?
Your default route is handled by "RouteConfig.cs" which is in the folder "App_Start"
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
In fact, I never use this RouteConfig anymore. I prefer using attributeRouting. It allows to define the routes via an attribute above your controller. http://attributerouting.net/ or more precisely http://attributerouting.net/#defining-routes
It depends on what your 'root controller' is called and what the name of your landing action is. if it is MyController.Index() then you would do
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "My", action = "Index", id = UrlParameter.Optional }

Categories

Resources