c# - HttpGet 在 Mvc 核心 2.0 中返回 404

标签 c# asp.net-core asp.net-core-mvc-2.0

public class SurveyController : Controller
{
    public IActionResult Index()
    {
        var surveys = new List<int>{1};
        return View(Surveys);
    }

    [HttpGet("conditions")]
    public IActionResult GetConditions()
    {
        List<int> Conditions = new List<int{1};
        return View("Conditions",Conditions);
    }
}

现在浏览量低于

Views/Sur​​vey/Index.cshtml

Views/Sur​​vey/Conditions.cshtml

路线 Survey/conditions 正在返回 404

有人知道吗?

我的 startup.cs 是 -

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseAuthentication();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });   
}

最佳答案

你对 Action 的属性:

[HttpGet("conditions")]

指定您希望 URL 为 http://whatever/conditions。相反,你应该使用:

[HttpGet("/survey/conditions")]

如果您希望以更好的粒度控制路由,则应改用 Route 属性。例如:

[Route("[controller]")] //Set the prefix for subsequent route attributes
public class SurveyController : Controller
{
    [Route("conditions")]
    public IActionResult GetConditions()
    {
        List<int> Conditions = new List<int{1};
        return View("Conditions",Conditions);
    }
}

引用 Routing to Controller Actions

关于c# - HttpGet 在 Mvc 核心 2.0 中返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48384406/

相关文章:

c# - 如何最好地在两个 ASP.NET Core 服务器应用程序之间进行通信?

c# - .NET Core 在另一个单例服务中注入(inject)单例服务

c# - InvalidOperationException : Cannot resolve scoped service 'Microsoft. AspNetCore.Identity.UserManager .NET 核心 2.0

c# - visual studio 2017 发布 asp.net core

c# - 在 EF Core 中指定 Azure SQL Server 版本而不破坏本地开发

c# - 多部分/表单数据请求——上传 pdf 导致生成空白文件

c# - 使用 Html Agility Pack 的类在跨度后获取文本

c# - 从数据库中填充数据时,如何在下拉列表中添加选项 "Any"?

asp.net-core - 在 .net Core 中像表单例份验证一样加密密码