Search code examples
c#visual-studio-2017asp.net-core-mvcasp.net-mvc-routingasp.net-core-2.0

ASP .Net Core MVC Routing Not Working in Visual Studio 2017 after changing "applicationUrl"


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

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:

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();
    }
}

Solution

  • The only workaround that I have now is to add suffix to the "applicationUrl" to be "http://localhost:61541/web" so I can have the routing fixed in Visual Studio.

    Here are the results

    Note: this solution may cause Ajax call debugging problems. Which means you may not be able to debug Ajax calls in server side. But to avoid this issue you can start the Ajax Url with the suffix like '/web/...'