MVC属性路由不工作 [英] MVC Attribute Routing Not Working
问题描述
我是比较新的MVC框架,但我有与利用AttributeRouting(封装的NuGet)的API控制器功能的Web项目 - 但是,我开始另一个项目,它只是不希望遵循的路线我落实到位。
控制器:
公共类BlazrController:ApiController
{
私人只读BlazrDBContext的DbContext = NULL;
私人只读IAuthProvider authProvider = NULL; 公共常量字符串HEADER_APIKEY =apikey;
公共常量字符串HEADER_USERNAME =用户名; 私人布尔CheckSession()
{
IEnumerable的<串GT; TMP = NULL;
清单<串GT; apiKey = NULL;
清单<串GT;的userName = NULL; 如果返回false(Request.Headers.TryGetValues(HEADER_APIKEY,出TMP)!);
apiKey = tmp.ToList(); 如果返回false(Request.Headers.TryGetValues(HEADER_USERNAME,出TMP)!);
的userName = tmp.ToList(); 的for(int i = 0; I< Math.Min(apiKey.Count(),userName.Count());我++)
如果返回false(authProvider.IsValidKey(用户名[I],apiKey [I])!); 返回true;
} 公共BlazrController(BlazrDBContext分贝,IAuthProvider AUTH)
{
的DbContext = DB;
authProvider =权威性;
} [GET(/ API / Q /用户)]
公共IEnumerable的<串GT;得到()
{ 返回新的字符串[] {值1,值2};
} [GET(API / Q / usersauth)]
公共字符串GetAuth()
{
如果回归您没有权限(CheckSession()!); 回归您被授权;
}
}
AttributeRoutingConfig.cs
公共静态类AttributeRoutingConfig
{
公共静态无效的RegisterRoutes(RouteCollection路线)
{
//见http://github.com/mccalltd/AttributeRouting/wiki了更多的选择。
//要在本地使用内置的ASP.NET开发服务器调试线路,去/routes.axd routes.MapAttributeRoutes();
} 公共静态无效的start()
{
的RegisterRoutes(RouteTable.Routes);
}
}
的Global.asax.cs:
//注意:有关启用IIS6 IIS7或经典模式的指令,
//访问 http://go.microsoft.com/?LinkId=9394801
公共类WebApiApplication:System.Web.HttpApplication
{
保护无效的Application_Start()
{
AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
当我尝试浏览/ API / Q /用户 - 我得到一个404未找到错误。如果我改变路线是/ API / blazr /用户 - 我得到多个动作的错误,不能够确定要采取的行动。
。任何帮助是AP preciated - (!我需要学习)我真的只需要一个小的微调,以找出其中的问题是,没有必要完全解决它为我
感谢
修改
routes.axd:
API / {控制器} / {ID}
{}资源个.axd / {*} PATHINFO
{控制器} / {行动} / {ID}
你不仅要有呼叫 routes.MapMvcAttributeRoutes()
在 App_Start \\ RouteConfig.cs
文件,它的必须出现在的定义你的默认路由!我之前和无视 {}资源的{个.axd * PATHINFO}
之后添加它。刚刚发现了试图获得属性的路由与我的MVC 5个网站的正常工作。
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapMvcAttributeRoutes(); routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=账户,行动=索引,ID = UrlParameter.Optional}
); }
I'm relatively new to the MVC framework but I do have a functioning Web Project with an API controller that utilizes AttributeRouting (NuGet package) - however, I'm starting another project and it just does not want to follow the routes I put in place.
Controller:
public class BlazrController : ApiController
{
private readonly BlazrDBContext dbContext = null;
private readonly IAuthProvider authProvider = null;
public const String HEADER_APIKEY = "apikey";
public const String HEADER_USERNAME = "username";
private Boolean CheckSession()
{
IEnumerable<String> tmp = null;
List<String> apiKey = null;
List<String> userName = null;
if (!Request.Headers.TryGetValues(HEADER_APIKEY, out tmp)) return false;
apiKey = tmp.ToList();
if (!Request.Headers.TryGetValues(HEADER_USERNAME, out tmp)) return false;
userName = tmp.ToList();
for (int i = 0; i < Math.Min(apiKey.Count(), userName.Count()); i++)
if (!authProvider.IsValidKey(userName[i], apiKey[i])) return false;
return true;
}
public BlazrController(BlazrDBContext db, IAuthProvider auth)
{
dbContext = db;
authProvider = auth;
}
[GET("/api/q/users")]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
[GET("api/q/usersauth")]
public string GetAuth()
{
if (!CheckSession()) return "You are not authorized";
return "You are authorized";
}
}
AttributeRoutingConfig.cs
public static class AttributeRoutingConfig
{
public static void RegisterRoutes(RouteCollection 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.MapAttributeRoutes();
}
public static void Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
Global.asax.cs:
// Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
When I try to navigate to /api/q/users - I get a 404 not found error. If I change the routes to be "/api/blazr/users" - I get an error about multiple actions and not being able to determine which action to take.
Any help is appreciated - I really just need a small nudge to figure out where the issue is, no need to solve it completely for me (I need to learn!)
Thanks
EDIT
routes.axd:
api/{controller}/{id}
{resource}.axd/{*pathInfo}
{controller}/{action}/{id}
Not only do you have to have the call to routes.MapMvcAttributeRoutes()
in your App_Start\RouteConfig.cs
file, it must appear before defining your default route! I add it before that and after ignoring {resource}.axd{*pathInfo}
. Just found that out trying to get attribute routing to work correctly with my MVC 5 website.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
}
这篇关于MVC属性路由不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!