简体   繁体   English

多个C#WebAPI API路由-将API方法限制为其中一种路由

[英]Multiple C# WebAPI API routes - restrict API method to one of the routes

I have multiple routes for my API like 我的API有多种路由,例如

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{token}/{controller}/{action}",
    defaults: null,
    constraints: null,
    handler: HttpClientFactory.CreatePipeline(
                new HttpControllerDispatcher(config),
                new DelegatingHandler[] { new ApiTokenValidator() })
);
config.Routes.MapHttpRoute(
    name: "LoginApi",
    routeTemplate: "api/{controller}/{action}",
    defaults: null,
    constraints: null,
    handler: HttpClientFactory.CreatePipeline(
                new HttpControllerDispatcher(config),
                new DelegatingHandler[] { new ApiLoginHandler() })
);

How can I make sure that a method in my APIController only can be used by for example the LoginApi route/handler? 我如何确保例如LoginApi路由/处理程序只能使用API​​Controller中的方法?

Like Uriil commented on the question, the answer is to use attribute routing. 就像Uriil在问题上发表的评论一样,答案是使用属性路由。

As of http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#add-routes http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#add-routes开始

In my Example I use it like this to restrict the AddUser method to only be used with the wordpress api 在我的示例中,我这样使用它来限制AddUser方法只能与wordpress api一起使用

[Route("api/wordpress/shared/AddUser")]
[HttpPost]
public Object AddUser(string username)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM