Search code examples
asp.net-core.net-corerazor-pagesiis-10

Endpoints not being discovered in PageModel when Published to IIS10: Http Response 404 .Net Core RazorPages


When debugging in IIS Express all endpoints are reachable via GET. When published to IIS10 I can navigate to the page public void OnGet() is being called and renders the razor page. When calling ./MyPage/Partial on server IIS10 I receive a 404 Not Found error and this does not happen on IIS Express in Visual Studio.

 public class IndexModel : PageModel
    {
        [BindProperty]
        public MyModel MyModel { get; set; }

        [HttpGet]
        public void OnGet()
        {
            ...
        }

        [HttpGet]
        public IActionResult OnGetPartial([FromQuery] int id)
        {
            ...
        }
    }

I have followed the instructions on https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2 and my best guess is that I need to configure these routes as per https://learn.microsoft.com/en-us/aspnet/core/razor-pages/razor-pages-conventions?view=aspnetcore-2.2

Although my question is why in IIS Express I can call javascript jquery $.load('./MyPage/Partial?id=1') and it works fine and when published it returns a 404 error? And what would be the specific solution?

EDIT: in my Index.cshtml I have the following @page "{handler?}" at the top in order to handle the custom REST methods.


Solution

  • In order to solve this I followed the instructions from https://learn.microsoft.com/en-us/aspnet/core/razor-pages/razor-pages-conventions?view=aspnetcore-2.2 in the file Startup.cs or whichever class you are using in Program.cs via

    WebHost.CreateDefaultBuilder(args)
                    .UseKestrel()
                    .UseStartup<Startup>();
    

    In the method in the file Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
       services.AddMvcCore().AddRazorPages(options => options.Conventions.AddPageRoute("/MyPage", "/MyPage/Partial/{id}")).AddRazorViewEngine().AddViews();
    
       // Other service code impl. here
    }