找到你要的答案

Q:HandleUnknownAction in ASP.NET 5

Q:handleunknownaction ASP.NET 5

What's an equivalent of ASP.NET MVC 5

Controller.HandleUnknownAction() 

in ASP.NET MVC 6 / ASP.NET 5?

什么是一个相当于ASP.NET MVC 5

Controller.HandleUnknownAction() 

在ASP.NET MVC 6 / ASP.NET 5?

answer1: 回答1:

There's no real equivalent.

Action Selection in MVC5/WebAPI2 was a three stage process: 1. Run the routes 2. Select a controller 3. Select an action

In MVC6, step 2 is gone. Actions are selected directly using route values - you'll notice that Controller.BeginExecute is gone as well. Controllers are 'thin' now.

You can simulate this behavior if you want by using a route that goes directly to your action in question.

Define an action called HandleUnknownAction in your controller routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); routes.MapRoute("unknown_action", "{controller}/{*params}", defaults: new { action = "HandleUnknownAction"});

没有真正的等价物。

Action Selection in MVC5/WebAPI2 was a three stage process: 1. Run the routes 2. Select a controller 3. Select an action

在mvc6,2步走。行动的选择直接使用路由值-你会发现controller.beginexecute消失了一样。控制器是'薄'现在。

如果你想使用一条直接指向你的行动的路线,你可以模拟这种行为。

Define an action called HandleUnknownAction in your controller routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); routes.MapRoute("unknown_action", "{controller}/{*params}", defaults: new { action = "HandleUnknownAction"});

answer2: 回答2:

An alternative approach is to simply define the unknown action as a parameter of your route:

[Route("[controller]")]
public class FooController : Controller
{

    [HttpGet("{viewName}")]
    public IActionResult HandleUnknownAction(string viewName)
    {
        return View(viewName);

    }
}

Using this approach, the url foo/bar would return the View bar.cshtml, foo/baz would return baz.cshtml etc.

另一种方法是简单地定义未知的行动作为您的路线参数:

[Route("[controller]")]
public class FooController : Controller
{

    [HttpGet("{viewName}")]
    public IActionResult HandleUnknownAction(string viewName)
    {
        return View(viewName);

    }
}

使用这种方法,URL foo/bar会回来看bar.cshtml,foo / Baz回到baz.cshtml等

asp.net-core  asp.net-core-mvc