gpt4 book ai didi

asp.net-mvc - 路由、打错 Action

转载 作者:行者123 更新时间:2023-12-02 05:04:44 25 4
gpt4 key购买 nike

我有两个 Action ,每当我显式调用第二个 Action 时,第一个 Action 仍会被调用。我需要能够在 mysite.com/api/Awesome/GetSomeExamples 中编写,而不总是触发 GetAllClasses

public class AwesomeController : Controller
{
public IEnumerable<myClass> GetAllClasses()
{
//...some stuff
}
public IEnumerable<string> GetSomeExamples()
{
//...some stuff
}
//... also have some more actions which take in one parameter
}

我的路由是这样的,但不工作

 public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "ActionApi",
url: "api/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

我得到的错误:发现多个与请求匹配的操作

最佳答案

您正在使用 Web API。

与普通 MVC 操作的不同之处在于 Web API 查看 HTTP 动词来确定要调用的操作。在这种情况下,您有两个以 Get

开头的方法

但是,这会触发一个错误:Multiple actions were found that match the request...它不应该同时调用这两个 Action 。

您需要的网址是:

mysite.com/api/Awesome/

从那里,Web API 将调用一个以 Get 开头的方法。您需要删除其他 get 方法并将其放在不同的 Controller 中。 Web API 假设每个 http 动词有一个 Action

关于asp.net-mvc - 路由、打错 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16441976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com