ASP MVC Simple Routing Optional Parameter - c#

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.

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

Disabling Default Routes on Asp.net Mvc

I am using Asp.net Mvc 5 with C#.
I want to disable default routing in my project. My map routes like;
routes.MapRoute(name: "News",
url: "haberler",
defaults: new { controller = "News", action = "Index"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When a user visits my website's news page, it's like;
www.domain.com/haberler
But also the user can visit the news page as below;
www.domain.com/news
I want to remove that "/news" or direct to seo-friendly url like; "/haberler"
So how can I disable default routing (Controller-Name-Convension) routing?
Instead of removing the "default" route, you can add a controller constraint to it
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = #"(Account|Manage|Home)" }
);
Like this, /news will return 404 Not Found.
In MVC, the convention is to map URLs to a particular Action on a particular Controller. Remove default route to disable default convention:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
www.domain.com/news will not match in any of your routes hence it will throw an error.
Try removing with this
RouteTable.Routes.Remove(RouteTable.Routes["NAME ROUTE YOU WISH TO RMOVE"]);
Hope this will helps you.

MVC Routing Inside a Folder

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.

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