如何为异常端点 api 配置设置路由?
how to set routing for unusual endpoint api configuration?
我正在构建一个新的 webapi 以供工作使用。我必须在哪里开发适合以下端点模式的 webapi2 应用程序
/file
/file/[data id]
/file/[data id]/documents
/file/[data id]/conditions
在我的控制器中,我有以下代码:
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");
}
}
我的路由配置如下所示:
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}
);
}
}
我的问题是如何配置路由以使其与端点配置相匹配
例如:“https://something.com/file/123412/document”
它是否命中了正确的控制器方法?
真的很困惑如何在这样的环境中设置路由。
你可以这样做-
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}
);
这意味着您正在为每个操作使用自定义路由。您仅将这些路由用于以 'file/' 开头的网址。你有很多不同的方法可以做到这一点。
如果您在这些模板中使用 {controller} 并删除默认值:controller = "File" 那么您将匹配其他控制器。
如果您可以重命名您的控制器操作以匹配您的路由,您可以将它们放入一个模式中,并在模板中使用 {action}。
routes.MapHttpRoute(
name: "File",
routeTemplate: "{controller}/{dataId}/{action}",
defaults: new {}
);
如果您重命名最后两个操作,这应该与它们相匹配。
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");
}
}
如果你在该路由中设置了一个默认动作,你也可以匹配你的第二个动作
routes.MapHttpRoute(
name: "File",
routeTemplate: "{controller}/{dataId}/{action}",
defaults: new { action = "UpdateDataByFile" }
);
我正在构建一个新的 webapi 以供工作使用。我必须在哪里开发适合以下端点模式的 webapi2 应用程序
/file
/file/[data id]
/file/[data id]/documents
/file/[data id]/conditions
在我的控制器中,我有以下代码:
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");
}
}
我的路由配置如下所示:
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}
);
}
}
我的问题是如何配置路由以使其与端点配置相匹配
例如:“https://something.com/file/123412/document”
它是否命中了正确的控制器方法?
真的很困惑如何在这样的环境中设置路由。
你可以这样做-
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}
);
这意味着您正在为每个操作使用自定义路由。您仅将这些路由用于以 'file/' 开头的网址。你有很多不同的方法可以做到这一点。
如果您在这些模板中使用 {controller} 并删除默认值:controller = "File" 那么您将匹配其他控制器。
如果您可以重命名您的控制器操作以匹配您的路由,您可以将它们放入一个模式中,并在模板中使用 {action}。
routes.MapHttpRoute(
name: "File",
routeTemplate: "{controller}/{dataId}/{action}",
defaults: new {}
);
如果您重命名最后两个操作,这应该与它们相匹配。
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");
}
}
如果你在该路由中设置了一个默认动作,你也可以匹配你的第二个动作
routes.MapHttpRoute(
name: "File",
routeTemplate: "{controller}/{dataId}/{action}",
defaults: new { action = "UpdateDataByFile" }
);