【问题标题】:Endpoints not being discovered in PageModel when Published to IIS10: Http Response 404 .Net Core RazorPages发布到 IIS10 时在 PageModel 中未发现端点:Http Response 404 .Net Core RazorPages
【发布时间】:2019-06-08 13:58:03
【问题描述】:

在 IIS Express 中调试时,所有端点都可以通过 GET 访问。当发布到 IIS10 时,我可以导航到正在调用的页面 public void OnGet() 并呈现剃刀页面。在服务器 IIS10 上调用 ./MyPage/Partial 时,我收到 404 Not Found 错误,这在 Visual Studio 中的 IIS Express 上不会发生。

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

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

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

我已按照https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2 上的说明进行操作,我最好的猜测是我需要按照https://docs.microsoft.com/en-us/aspnet/core/razor-pages/razor-pages-conventions?view=aspnetcore-2.2 配置这些路由

虽然我的问题是为什么在 IIS Express 中我可以调用 javascript jquery $.load('./MyPage/Partial?id=1') 并且它工作正常并且在发布时返回 404 错误?以及具体的解决方案是什么?

编辑:在我的 Index.cshtml 中,我在顶部有以下 @page "{handler?}" 以处理自定义 REST 方法。

【问题讨论】:

  • 1) ~/ 仅适用于 Razor 解析的代码。它不是官方的路径指示符——只是 Microsoft 用作“项目根目录”的简写。你应该只使用/MyPage/Partial?id=1。如果您要部署到虚拟目录,则需要在生产中相应地更新 URL。 2) 确保您的生产环境中确实存在带有该指示符的部分。换句话说,例如,您可能会基于失败的数据库查询返回 404,而不是一般的错误路由。
  • 就像我说的那样,它正在使用 IIS Express 和抱歉而不是 ~ 这是 jquery 解析路径的时期。我编辑了我的问题以反映这一点。
  • 我能够使用我认为是答案的方法来解决它,将此添加到您的 startup.cs services.AddMvcCore().AddRazorPages(options => options.Conventions.AddPageRoute("/MyPage", "/MyPage/Partial/{id}")).AddRazorViewEngine().AddViews();
  • 如果您找到答案,请在下方发布并接受。

标签: asp.net-core .net-core razor-pages iis-10


【解决方案1】:

为了解决这个问题,我按照 Startup.cs 文件中https://docs.microsoft.com/en-us/aspnet/core/razor-pages/razor-pages-conventions?view=aspnetcore-2.2 的说明或您在 Program.cs 中使用的任何类通过

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

在 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2018-05-05
    • 2019-12-18
    • 2019-12-13
    相关资源
    最近更新 更多