0
votes

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

2 Answers

4
votes

It looks like each time you make a change to the application url in Visual Studio it is writing to [project directory].vs\config\applicationhost.config. It adds a new "application" node under your "site" node. In my case it was /users and then /web, as per your recommendation. It just keeps adding new application paths with "virtualDirectory" nodes having path="/". It does not remove any of them. Each new "application" path will work. However, if the path you provide is a controller it is confusing.

From the VS user interface it seems reasonable to assume if you change the App Url your are changing a single value, not adding to a list of valid values.

Anyway, if you edit applicationhost.config to remove the "application"s under your site that you don't want, it should work as before.

1
votes

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/...'