ASP.NET Core 3.0 控制器路由不工作 [英] ASP.NET Core 3.0 Controller Routing Not Working

查看:55
本文介绍了ASP.NET Core 3.0 控制器路由不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Disclaimer: I'm new to ASP.NET Core / Razor / MVC and am starting out on the 3.0 preview.

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

What I want to do is have a "button" on my page that adds a new empty item to a list so that the user can input some values. From what I've read (which is quite a bit) it sounds like having a hyperlink point towards a controller is the right approach to this. I cannot get it to actually work though. Here's my code:

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

<a class="btn btn-success" asp-controller="Customer" asp-action="AddProduct">New Product</a>

控制器:

    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...";
    }

}

Startup.cs 路由是默认的:

        app.UseRouting(routes =>
        {
            routes.MapRazorPages();
        });

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

With this setup, if I click the start button, I see the URL change to the below, but nothing else happens (no break point is hit, for example):

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

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

I have tried to adding the route to specifically to the UseRouting code, like such:

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

但是当我这样做时,它似乎坏了,因为文本颜色发生了变化(从白色到黑色),而当我点击它时没有任何反应.

But when I do this, it seems to break, as the text color changes (from white to black) and nothing happens when I click it.

知道我哪里出错了吗?

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

I do have one more question - which is how do you access the model data from the controller?

推荐答案

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

See middle of page here: https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-core-3-0-preview-4/

在 Startup.cs 中配置:

In Startup.cs Configure:

        app.UseRouting();
        app.UseEndpoints(routes =>
        {
            routes.MapRazorPages();
            routes.MapFallbackToPage("/Home");
        });

这篇关于ASP.NET Core 3.0 控制器路由不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆