I added a new Controller called "ListController" in to MVC project
if I write in browser Url http://localhost:1509/list everything works fine
But if I write in browser Url "http://localhost:1509/List" I get Erorr page
The only difference is if the letter L in the URL is capitalized or lowercase letter
What causes it
Either case sensitive routing has been configured in the global.asax file without you knowing about it, or you are missing something.
Asp.net mvc routing urls are not case sensitive by default. "home", "HOME", "hOmE", etc.. are all the exact same as far as the mvc routing engine is concerned.
Here is the url where ScottGu explains the Asp.Net MVC url Routing Rules how they work.
ScottGu - ASP.Net MVC Framework: URL Routing
Related
I have an ASP.NET Core 3.1 MVC router question.
When I change index route to [Route("Index")] in HomeController enter image description here, and then enter the url localhost:5000/index in web browser, it works!
But in my ASP.NET Core MVC project, the default startup url is localhost:5000/home/index enter image description here.
Question
How can I change the default startup url to localhost:5000/index?
I wish use localhost:5000/home/login to visit the login function in the HomeController.
Add following route before your default route.
endpoints.MapControllerRoute(
name: "customindex",
pattern: "index",
defaults: new { controller="Home", action="Index"});
});
pattern can support default value but it works with common scenario like controller/action but if you want specific pattern then you have to specify pattern and also set defaults so those will supply as route value in order to identify action to call.
I have a hybrid web application that has WordPress as the root site with an aspnet core web application as a virtual directory. The IIS structure looks something like:
+- DefaultWebSite (hosts WordPress)
| +- web (virtual direcotry that hosts the aspnet core site)
Within the aspnet core site, when I redirect to a page or use tag helpers the url includes "/web/blahblah" which makes sense. However, I don't want that. How do I force the aspnet core site to ignore the virtual directory path and generate urls like "/blahblah" without web?
You can add the routing middleware in such a way that it knows where the controller name comes in the URL.
For Ex. if your asp.net core root url is - http : // www.mysite.com / web
You can configure routing like below:
routes.MapRoute(
name: "default_route",
template: "/web/{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
I have created a web application using ASP.NET Core 2.1 MVC.
And currently i am having an issue with routing when the application is published.
The url format where the application will be published is this : https://hostername.com/{some_parameter}
All the generated url-s from my application should be "attached" to the above mentioned url.
So i need to have a routing like this :
https://hostername.com/{some_parameter}/{controller}/{action}/{id}
Some examples :
- https://hostername.com/ApplicationName/Home/Profile
- https://hostername.com/ApplicationName/Home/Settings
- https://hostername.com/ApplicationName/FAQ etc...
My solution for this after reading couple of questions/solutions on stackoverflow :
Changed the default route to
routes.MapRoute(
name: "default",
template: $"{{parameter={settings.PrefixURL}}}/{{controller=Home}}/{{action=Index}}/{{id?}}");
where settings.PrefixURL => it's the some_parameter and it's value it's dynamic.
Problem i am facing =>
Doubled parameters in url, for example :
- https://hostername.com/ApplicationName/Home/Home/Profile
- https://hostername.com/ApplicationName/ApplicationName/FAQ
When tested locally that configuration of default routing worked perfectly, but after publishing the routing still works but the url is wrong.
What could be causing the issue?
Would creating Areas solve the routing to that kind of url?
Thank you.
What did the work for me was editing the path on web.config file and leaving the rest of soultion untouched.
From path="*" to path="/ApplicationName" where ApplicationName is the desired routing parameter.
I am in the process of moving old website to a new ASP .Net MVC website. The old site has pretty bad url naming scheme. I would love to ignore the old ways and just create new urls, however, A LOT of links point to the old links for SEO. Therefore, I have to maintain the older url.
So let's say this is the old url:
web.com/items/products/Hello-World-Hyphens
How do I input that on MVC?
I got ProductsController:
ActionResult HelloWorldHyphens() { return View(); }
Which will output to web.com/products/HelloWorldHyphens
However, I need it written in the old ways. Starting with /items/ and having hyphens in controller name.
Is there a way I can do something like this?
[OutputUrl="/items/products/Hello-World-Hyphens"]
ActionResult HelloWorldHyphens()
As you are moving to a new ASP MVC website then you can take advantage of attribute routing in MVC 5.
If you add attribute routing when you register your routes:
routes.MapMvcAttributeRoutes();
Then you can add routes on methods:
[Route("/items/products/Hello-World-Hyphens")]
ActionResult HelloWorldHyphens()
What string does come to ASP.NET MVC route if I request http://subdomain.domain.com/pathAndQuery?
Is route applied to the whole string or to pathAnQuery part only?
Routes are applied just to the pathAndQuery.
This is why your routes work on your development and production sites despite the fact that they have different URLs.