0
votes

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://docs.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://docs.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.

1
1) The ~/ only works in Razor-parsed code. It's not an official path designator - just something Microsoft uses as shorthand for "project root". You should just use /MyPage/Partial?id=1. If you're deploying to a virtual directory, you'll need to update the URLs accordingly in production. 2) Ensure that a partial with that designator actually does exist in your production environment. In other words, you may be returning 404 based on a failed DB query, for example, rather than a bad route in general. - Chris Pratt
Like I said it is working, with IIS Express and sorry instead of ~ it was a period which jquery resolves the path. I edited my question to reflect that. - Skyler Sanders
I was able to solve it using what I thought was an answer add this to your startup.cs services.AddMvcCore().AddRazorPages(options => options.Conventions.AddPageRoute("/MyPage", "/MyPage/Partial/{id}")).AddRazorViewEngine().AddViews(); - Skyler Sanders
If you find the answer, post it below and accept it. - Lex Li

1 Answers

0
votes

In order to solve this I followed the instructions from https://docs.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
}