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/Survey/Index.cshtml

Views/Survey/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?}");
    });
}

最佳答案

您对动作具有的属性:

[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 Core 2.0中返回404,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48384406/

10-11 02:04