我有一个遗留项目,有一个单一的IHttpHandler实现类,使用巨大的switch语句等路由所有请求。我试图引入属性路由与ApiControllers,但第一个始终具有优先级。是否可以配置系统(代码或IIS),以便Web ApiController优先于我的单个IHttpHandler实现类?在IIS中,我首先将我的AttributeRouting放入,然后是所有的aspx,但仍然没有先处理Web Api Controller ..不管我做什么(在同一个项目下都有)。我不想介绍一个单独的项目。如何优先使用IHabpHandler的Web Api控制器?
编辑:有一个IHttpModule根据api后的内容/将其路由到特定的ashx文件。其中之一是描述的..
编辑2:更具体地说:如果uri没有过滤的东西[文件,消息,属性...]的列表,它被路由到Resource.aspx
所以API /文件,API /消息,API /属性会从其他.ashx的文件处理 - 否则交通去Resource.ashx ...
结果有API /端点1请求,api/endpoint2,api/endpoint3 将全部转到Resource.aspx。问题是如何将api/endpoint3路由到下面描述的API控制器。 感谢
简化的代码架构:
//SolutionName/Api/MyModule.cs (Legacy Code)
//this routes based on what is after api/ to Resource.ashx or other ashx files
public class MyModule : IHttpModule {
//if url doesn't contain [file,message,property ...] route to Resource.ashx
}
//SolutionName/API/Resource.ashx (Legacy Code)
//this is hit at any request solutionname/api/anything
public class DefaultHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context) {
String APIBranch = parse(context);
switch(APIBranch)
{
case "endpoint1": methodOne(); break;
case "endpoint2": methodTwo(); break;
[...]
default: throw Exception(); break;
}
}
}
//SolutionName/API/App_Start/AttributeRoutingHttpConfig.cs
public static class AttributeRoutingHttpConfig
{
public static void RegisterRoutes(HttpRouteCollection routes)
{
// See http://github.com/mccalltd/AttributeRouting/wiki for more options.
// To debug routes locally using the built in ASP.NET development server, go to /routes.axd
routes.MapHttpAttributeRoutes();
}
public static void Start()
{
RegisterRoutes(GlobalConfiguration.Configuration.Routes);
}
}
//SolutionName/API/Controllers/MyController.cs
//this should have been hit for a GET on solutionname/api/endpoint3/id
[RoutePrefix("endpoint3")]
public class MyController : ApiController
{
private IModelDao modelDao;
MyController(IModelDao modelDao){
this.modelDao = modelDao;
}
[Route("{id}")]
[HttpGet]
public Model GetSomething(int id)
{
Model model = modelDao.GetSomething(id);
return model;
}
}
'HttpHandlers'的API控制器被击中之前的工作。处理程序用于检查和潜在拦截或重定向('采取行动')给定的资源请求。反过来也不行。你必须从处理程序的switch语句中取出'default'才能使其工作。但是,您可以使用web api执行相同的操作 - 您可以明确指定配置中的所有可用路由,并使用默认路由将它们引导至返回错误的路由。 – ps2goat
现在我已经想了一下,你可能会检查配置的API路线并让它们通过。现在我想尝试它... – ps2goat
@ ps2goat我可以有一个HttpModule,它会打我的ApiController,而不是 - 虽然我不知道这将如何..我希望这不会有性能打击.. –