ASP.NET Core 3.0 控制器路由不工作

Yec*_*ats 5 asp.net-mvc razor asp.net-core

免责声明:我是 ASP.NET Core / Razor / MVC 的新手,正在开始使用 3.0 预览版。

我想要做的是在我的页面上有一个“按钮”,将一个新的空项目添加到列表中,以便用户可以输入一些值。从我所读到的(相当多),听起来像拥有指向控制器的超链接点是正确的方法。我无法让它实际工作。这是我的代码:

指向控制器/动作的链接:

<a class="btn btn-success" asp-controller="Customer" asp-action="AddProduct">New Product</a>
Run Code Online (Sandbox Code Playgroud)

控制器:

    public class CustomerController : Controller
{
    public void AddProduct()
    {

        var tmp = "";

    }

    public string Index()
    {
        return "This is my default action...";
    }

    public string Welcome()
    {
        return "This is the Welcome action method...";
    }

}
Run Code Online (Sandbox Code Playgroud)

Startup.cs 路由是默认的:

        app.UseRouting(routes =>
        {
            routes.MapRazorPages();
        });
Run Code Online (Sandbox Code Playgroud)

使用此设置,如果我单击开始按钮,我会看到 URL 更改为以下内容,但没有其他任何反应(例如,未命中断点):

https://localhost:44358/Customers/Create?action=AddProduct&controller=Customer

我试图将路由添加到专门的 UseRouting 代码中,如下所示:

            app.UseRouting(routes =>
        {
            routes.MapRazorPages();
            routes.MapControllerRoute(
                name: "Customer",
                template: "{controller=Customer}/{action=Welcome}");
        });
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,它似乎中断了,因为文本颜色会发生变化(从白色变为黑色),并且当我单击它时没有任何反应。

知道我哪里出错了吗?

我还有一个问题——你如何从控制器访问模型数据?

GGl*_*and 2

请参阅此处的页面中间:https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-core-3-0-preview-4/

在Startup.cs中配置:

        app.UseRouting();
        app.UseEndpoints(routes =>
        {
            routes.MapRazorPages();
            routes.MapFallbackToPage("/Home");
        });
Run Code Online (Sandbox Code Playgroud)