带有.a​​sp扩展名的ASP.NET MVC路由

时间:2017-01-23 16:57:23

标签: asp.net asp.net-web-api

编辑:'重复'不工作 - 不确定为什么。

我需要能够支持如下所示的ASP.NET WebApi(v5.2.3.0)路由:

https://example.com/orders/orders.asp

第三方设备能够与我的系统进行交互。我试过像这样乱用RouteConfig:

routes.MapHttpRoute(
            name: "Orders",
            routeTemplate: "api/orders.asp",
            defaults: new { controller = "OrderConfirm", action = "Get", id = RouteParameter.Optional }
        );

但我总是得到404错误。我已经和#34; Route"装饰,像这样:

[Route("api/orders.asp")]

控制器代码:

public class OrdersConfirmController : ApiController
{
    [HttpGet]
    public HttpResponseMessage Get()
    {
        string orderContent = "blah, blah, blah";
        var response = Request.CreateResponse(HttpStatusCode.OK, string.Empty);
        response.Content = new StringContent(orderContent, Encoding.UTF8, "text/plain");
        return response;
    }
}

但仍然是404错误。

想法?

1 个答案:

答案 0 :(得分:1)

在路线表顶部添加新路线

config.MapHttpAttributeRoutes();

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

OrdersConfirmController添加属性:

[RoutePrefix("Orders")]
public class OrdersConfirmController : ApiController

和行动

[Route("orders.asp")]
[HttpGet]
public HttpResponseMessage Get()

另外要在URL中添加点,您需要将其添加到web.config

<system.webserver>
    <modules runAllManagedModulesForAllRequests="true">

现在您可以通过http://example.com/Orders/orders.asp

来获得资源