I have created an empty ASP.NET Core Web Application and I added the MVC service and configuration to "Startup.cs" file
And in order to set my start up view "Users/Create", I have changed the "applicationUrl" in launchSettings.json file as following
"applicationUrl": "http://localhost:61541/Users/Create",
First Issue
I got the expected URL when running the visual studio "http://localhost:61541/Users/Create" but it shows me the "Home/Index" view (the default route)
Solution
Now I realized that I shouldn't change the "applicationUrl" value as MVC consider it the basic root. This is why I got the following behavior
- http://localhost:61541/Users/Create --> returns the default route "Home/Index"
- http://localhost:61541/Users/Create/Users/Create --> returns "Users/Create"
The Main Issue
When I decided to change the "applicationUrl" back to be "http://localhost:61541" and navigate to "http://localhost:61541/Users/Create", I got again the "Home/index" view (the default route)
Also I got the following behavior:
- http://localhost:61541/Users/Create --> returns "Home/Index"
- http://localhost:61541/Users/Index --> returns "Users/Index"
- http://localhost:61541/Users/Create/Users/Create --> returns "Users/Create"
Note: everything is working as expected after deploying the solution into IIS. But in Visual Studio the behavior still odd for me
My Question
I am asking why the routing inside Visual Studio (with IIS express) not working as before? although I changed the "applicationUrl" back to its original value?!
Here is the solution code
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
Then I added two controllers with two views for each
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Create()
{
return View();
}
}
public class UsersController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Create()
{
return View();
}
}