3
votes

I'm struggling to get my ASP.Net core 1 (asp.net 5 / MVC 6) app running within IIS on my webserver. I've followed the guides and done the following on the server

  • Install ASP.Net 5 via get.asp.net
  • Install HttpPlatformHandler 1.2

I've checked that I can run dnx on the server and that the compiled bits are 64 bit and that the application pool is "No Managed Code" and running as 64 bits.

I can run the app on the webserver by running web.cmd and navigating to http://localhost:5000 (or whatever port), however when I try and setup the app as an application within the Default Website and browse to it (e.g. http://localhost/MyMVC6App) I get a 404 error. I've checked that the physical path is pointing to /MyMVC6App/wwwroot. I've also checked that the webserver/handlers section is unlocked.

I've also created a vanilla ASP.Net 5/Core 1 app and get the same 404 error on 2 different servers!

Here is my configure method:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseApplicationInsightsRequestTelemetry();

            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseIISPlatformHandler();

            app.UseApplicationInsightsExceptionTelemetry();

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

Any ideas?

1
I've also just found that I can run the app if I create a new website (not web application) and host it as the only web application. However as I have other web apps (MVC 5 and below) I can't do this as I would need to run the new website on a different port. - Calanus
Can you confirm when you try to access the URL, does the IIS AppPool start dnx.exe? If it's not then it means the issue is with the HttpPlatform config in your web.config file. The path in web.config uses relative paths I think. If it is starting dnx.exe and it dies soon after you should enable stdoutLogEnabled="true" in httpPlatform config to find out why. - Muqeet Khan
info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0] User profile is available. Using 'C:\Windows\system32\config\systemprofile\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest. Hosting environment: Production Now listening on: localhost:8465 Application started. Press Ctrl+C to shut down. info: Microsoft.AspNet.Hosting.Internal.HostingEngine[1] Request starting HTTP/1.1 GET localhost/Extranet info: Microsoft.AspNet.Hosting.Internal.HostingEngine[2] Request finished in 0.0656ms 404 - Calanus
Could you also paste in your configure method? I will see if I can reproduce on my end ... Just noticed you are running this as an app on IIS ... They have said that's a known thing... But Add your MVC6app as the base for routes ... Like /MVC6APP/{controller=Home}/... - Muqeet Khan
Holy Moly - if I change my route template to Extranet/{controller=Home}/{action=Index}/{id?}"); it works - the css is screwed but it works - how come!? - Calanus

1 Answers

1
votes

The easiest way to resolve this would be to add the IIS Application name as the base of your route in MVC if your static files are hosted out of the ASPNET Core app.

Another way would be by adding a simple middleware class to strip out the IIS application name and make the request look transparent to any module below it. I havent tested the code below but:

using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;

namespace MyMVC6App
{
    // You may need to install the Microsoft.AspNet.Http.Abstractions package into your project
    public class RemoveIISAppNameMiddleware
    {
        private readonly RequestDelegate _next;

        public RemoveIISAppNameMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public Task Invoke(HttpContext httpContext)
        {
            var newRequestPath = new PathString();
            var requestPathToIgnore = new PathString("/MyMVC6App");

            if (httpContext.Request.Path.StartsWithSegments(requestPathToIgnore))
            {
                httpContext.Request.Path.StartsWithSegments(requestPathToIgnore, out newRequestPath);
                httpContext.Request.Path = newRequestPath;
            }
            return _next(httpContext);
        }
    }

    // Extension method used to add the middleware to the HTTP request pipeline.
    public static class RemoveIISAppNameMiddlewareExtensions
    {
        public static IApplicationBuilder UseRemoveIISAppName(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<RemoveIISAppNameMiddleware>();
        }
    }
}

and then in your configure method after app.UseIISPlatformHandler() you would call app.UseRemoveIISAppName(). In this case you wouldn't need to have any additional path in MVC routes.