ASP.net网络API 2路由属性不工作 [英] ASP.net web api 2 Route-Attribute not working

查看:150
本文介绍了ASP.net网络API 2路由属性不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题,我的路由属性不能正常工作。

我有以下作用:

  [HTTPGET]
[路线(〜API /管理/模板/ {文件名})]
公众的Htt presponseMessage模板(字符串文件名)
{
    返回CreateHtmlResponse(文件名);
}
 

和我想访问诸如 ... / API /管理/模板/ login.html的的动作,从而使模板获得的的login.html 传递的文件名。

但我alsways得到:没有HTTP资源被发现,相匹配的请求URI'的http://本地主机:50121 / API /管理/模板/ login.html的

以下要求作品: /api/admin/template?fileName=login.html

有谁知道,我在做什么毛病我的路由?

修改

我的路由配置

  config.Routes.MapHttpRoute(
                    API默认,API / {控制器} / {行动},
                    新的id {= RouteParameter.Optional});
 

解决方案

您必须调用 MapHttpAttributeRoutes()这样的框架将能够穿行的属性和在应用程序启动注册相应的路线:

 公共静态类WebApiConfig
{
    公共静态无效的注册(HttpConfiguration配置)
    {
        config.MapHttpAttributeRoutes();

        //你可以添加手动路线以及
        //config.Routes.MapHttpRoute(...
    }
}
 

请参阅 MSDN

I've the following problem, my route attribute is not working.

I have the following action:

[HttpGet]
[Route("~api/admin/template/{fileName}")]
public HttpResponseMessage Template(string fileName)
{
    return CreateHtmlResponse(fileName);
}

and i want to access the action like .../api/admin/template/login.html, so that Template get login.html passed as the file name.

But i alsways get: No HTTP resource was found that matches the request URI 'http://localhost:50121/api/admin/template/login.html'.

The following request works: /api/admin/template?fileName=login.html

Does anyone know, what i am doing wrong with my routing?

EDIT:

My route configuration

config.Routes.MapHttpRoute(
                    "API Default", "api/{controller}/{action}",
                    new { id = RouteParameter.Optional });

解决方案

You have to call MapHttpAttributeRoutes() so that the Framework will be able to walk through your attributes and register the appropriate routes upon application start:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        // you can add manual routes as well
        //config.Routes.MapHttpRoute(...
    }
}

See MSDN

这篇关于ASP.net网络API 2路由属性不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆