0

为了便于说明,我举一个简单的例子如下。

审查.cshtml.cs:

public class ReviewModel : PageModel
{
    public void OnGet(int? id)
    {
        if (id == null)
            ViewData["X"] = "null";
        else
            ViewData["X"] = id;
    }
}

审查.cshtml:

@page 

@model ReviewModel

<!DOCTYPE html>
<html>
<body>
    X = @ViewData["X"]
</body>
</html>

当我导航到

  • localhost/Review/1--------------- 404。
  • localhost/Review/xyz--------------- 404。
  • localhost/Review/--------------- X=null

  • localhost/Review?id=1--------------- X=1

  • localhost/Review?id=--------------- X=null
  • localhost/Review?id=xyz--------------- X=null

我试图使用 Visual Studio Debugger 来计算id抛出 404 页面的值,但我无法进入。

传入请求以 404 结尾时如何知道 id 的值?

4

1 回答 1

3

I think it has to do with routing, you should be able to make your own route with something like this in your Startup.cs:

options.Conventions.AddPageRoute("/Review", "Review/{id?}");

See https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/razor-pages-convention-features

于 2018-01-04T17:36:45.007 回答