简体   繁体   English

如何为异常的端点api配置设置路由?

[英]how to set routing for unusual endpoint api configuration?

I am building a new webapi for use at work. 我正在构建一个新的webapi以便在工作中使用。 Where I have to develop a webapi2 application that fits the following endpoint schema 我必须在哪里开发适合以下端点模式的webapi2应用程序

/file
/file/[data id]
/file/[data id]/documents
/file/[data id]/conditions

In my controller I have the following code: 在我的控制器中,我有以下代码:

public class FileController : ApiController
{
    [HttpPost]
    public HttpResponseMessage ImportFile()
    {
        var act = Request.Headers.Accept.ToString();

        // test content type for "application/vnd.exp"

        return Request.CreateResponse(HttpStatusCode.OK, $"Successful import @ {DateTime.Now}");
    }

    [HttpPatch]
    public HttpResponseMessage UpdateDataByFile(string dataId)
    {
        var act = Request.Headers.Accept.ToString();


        return Request.CreateResponse(HttpStatusCode.OK, "Successful save");
    }

    [HttpPatch]
    public HttpResponseMessage UpdateDataIntake(string dataId)
    {
        var act = Request.Headers.Accept.ToString();

        return Request.CreateResponse(HttpStatusCode.OK, "Successful save");
    }

    [HttpGet]
    public HttpResponseMessage GetDataConditionsForUser(string dataid)
    {
        var act = Request.Headers.Accept.ToString();

        return Request.CreateResponse(HttpStatusCode.OK, "Successful get");
    }
}

My route config looks like the following: 我的路由配置如下所示:

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

        routes.MapHttpRoute(
            name: "Files1",
            routeTemplate: "{controller}/{action}"
        );

        routes.MapHttpRoute(
            name: "Files2",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new {id = RouteParameter.Optional}
        );


    }
}

My question is how do I configure the routing so that it matches the endpoint configuration 我的问题是如何配置路由,使其与端点配置匹配

eg: " https://something.com/file/123412/document " 例如:“ https://something.com/file/123412/document

and have it hit the correct controller method? 并击中了正确的控制器方法?

Really confused about how to set routing in an environment like this. 真的很困惑如何在这样的环境中设置路由。

You can do something like this- 你可以做这样的事情-

        routes.MapHttpRoute(
            name: "File",
            routeTemplate: "file",
            defaults: new { controller = "File", action = "ImportFile" }
        );

        routes.MapHttpRoute(
            name: "FileUpdate",
            routeTemplate: "file/{dataId}",
            defaults: new { controller = "File", action = "UpdateDataByFile" }
        );

        routes.MapHttpRoute(
            name: "FileDocuments",
            routeTemplate: "file/{dataId}/documents",
            defaults: new { controller = "File", action = "UpdateDataIntake" }
        );

        routes.MapHttpRoute(
            name: "FileConditions",
            routeTemplate: "file/{dataId}/conditions",
            defaults: new { controller = "File", action = "GetDataConditionsForUser" }
        );

        routes.MapHttpRoute(
            name: "Files1",
            routeTemplate: "{controller}/{action}"
        );
        routes.MapHttpRoute(
            name: "Files2",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new {id = RouteParameter.Optional}
        );

That means you are using a custom route for each Action. 这意味着您正在为每个操作使用自定义路由。 You are only using these routes for urls that start like 'file/'. 您仅将这些路由用于以“ file /”开头的网址。 You have a lot of different ways that you can do this. 您有许多不同的方法可以执行此操作。

If you use {controller} in these templates and remove default: controller = "File" then you will match for other controllers. 如果在这些模板中使用{controller}并删除默认值:controller =“ File”,则将与其他控制器匹配。

If you can rename your Controller Actions to match your routing you may be able to fit them into a pattern, and use the {action} in the template. 如果您可以重命名控制器动作以匹配您的路由,则可以使它们适应某种模式,并在模板中使用{action}。

routes.MapHttpRoute(
            name: "File",
            routeTemplate: "{controller}/{dataId}/{action}",
            defaults: new {}
        );

If you rename your last two Actions this should match them. 如果您重命名了最后两个动作,则这应该与它们匹配。


public class FileController : ApiController
{

    [HttpPatch]
    public HttpResponseMessage Documents(string dataId)
    {
        var act = Request.Headers.Accept.ToString();

        return Request.CreateResponse(HttpStatusCode.OK, "Successful save");
    }

    [HttpGet]
    public HttpResponseMessage Conditions(string dataid)
    {
        var act = Request.Headers.Accept.ToString();

        return Request.CreateResponse(HttpStatusCode.OK, "Successful get");
    }
}

And if you set a default action in that route, you can also match your second Action 如果您在该路线中设置了默认操作,则还可以匹配第二个操作

routes.MapHttpRoute(
            name: "File",
            routeTemplate: "{controller}/{dataId}/{action}",
            defaults: new { action = "UpdateDataByFile" }
        );

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

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