简体   繁体   English

带有 .asp 扩展名的 ASP.NET MVC 路由

[英]ASP.NET MVC route with .asp extension

EDIT: The 'duplicates' are not working - not sure why exactly.编辑:“重复”不起作用 - 不知道为什么。

I need to be able to support an ASP.NET WebApi (v5.2.3.0) route that looks like this:我需要能够支持如下所示的 ASP.NET WebApi (v5.2.3.0) 路由:

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

It's for a 3rd party device to be able to interact with my system.这是让第 3 方设备能够与我的系统进行交互。 I've tried messing around with the RouteConfig like this:我试过像这样弄乱 RouteConfig:

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

But I always get a 404 error.但我总是收到 404 错误。 I've messed around with the "Route" decoration, like this:我弄乱了“路线”装饰,如下所示:

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

Controller code:控制器代码:

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;
    }
}

But still the 404 errors.但是还是404错误。

Ideas?想法?

Add a new route at the top of route table在路由表顶部添加一条新路由

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 }
);

To OrdersConfirmController add attribute:OrdersConfirmController添加属性:

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

and for action和行动

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

Also to allow dot in URL you need to add this to web.config同样要允许 URL 中的点,您需要将其添加到 web.config

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

Now you can reach resourse by http://example.com/Orders/orders.asp现在您可以通过http://example.com/Orders/orders.asp访问资源

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

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