gpt4 book ai didi

c# - 具有多个获取和参数的 Asp.net mvc api

转载 作者:行者123 更新时间:2023-11-30 17:54:23 25 4
gpt4 key购买 nike

致谢,

我的 asp.net mvc4 研究项目有问题。我尝试创建具有多个 get 操作的 apicontroller,其中一些带有参数,另一些则没有。

我的应用程序

public class ContentController : ApiController
{
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories1()
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}

[System.Web.Http.HttpGet]
public HttpResponseMessage Categories2()
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}

[System.Web.Http.HttpGet]
public HttpResponseMessage Categories3(int i)
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}

[System.Web.Http.HttpGet]
public HttpResponseMessage Categories4(int i)
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
}

我的 apiConfig:

    public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ApiByActionWithId",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}"
);

config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null
);

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}

结果

对于/api/content/categories1,结果是 ["Toys","Games","Animals","Woob-Woob"]

对于/api/content/categories3 和/api/content/categories3/1 结果 - “没有找到匹配请求 URI 'http://localhost:51200/api/content/categories3' 的 HTTP 资源。”

不知道该怎么处理

最佳答案

我认为它失败是因为 api 操作中的参数名称与路由中的参数名称不匹配。

改变你的第一条路线:

config.Routes.MapHttpRoute(
name: "ApiByActionWithId",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

以下内容:

config.Routes.MapHttpRoute(
name: "ApiByActionWithId",
routeTemplate: "api/{controller}/{action}/{i}",
defaults: new { i = RouteParameter.Optional }
);

看看是否可行。

一般来说,更具体的路由应该在 Register 方法的顶部。随着更通用的路线在底部作为包罗万象。

考虑如下 Controller :

public class UserSettingsController : ApiController
{
[HttpGet]
public HttpResponseMessage GetAll();
[HttpGet]
public HttpResponseMessage Get(string key);
[HttpGet]
public HttpResponseMessage Set(string key, string value);
[HttpGet]
public HttpResponseMessage Restore(string key);
}

要设置路线,它需要以下路线。请注意,它们是有序的,因此如果 api 调用中没有键,则将调用 GetAll 方法。如果前 2 个路由被切换,那么 GetAll 版本将永远不会被命中,因为对/api/settings 的调用将转到 Get 方法并且键将被设置为 null。

        config.Routes.MapHttpRoute(
name: "user/settings",
routeTemplate: "user/settings",
defaults: new { controller = "UserSettings", action = "GetAll" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);

config.Routes.MapHttpRoute(
name: "user/settings/{key} (get)",
routeTemplate: "user/settings/{key}",
defaults: new { controller = "UserSettings", action = "Get" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);

config.Routes.MapHttpRoute(
name: "user/settings/{key} (set)",
routeTemplate: "user/settings/{key}",
defaults: new { controller = "UserSettings", action = "Set" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Put) }
);

config.Routes.MapHttpRoute(
name: "user/settings/{key}/restore",
routeTemplate: "user/settings/{key}/restore",
defaults: new { controller = "UserSettings", action = "Restore" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
);

关于c# - 具有多个获取和参数的 Asp.net mvc api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16066815/

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