MVC 属性路由不起作用 [英] MVC Attribute Routing Not Working
问题描述
我对 MVC 框架比较陌生,但我确实有一个功能强大的 Web 项目,其中有一个使用 AttributeRouting(NuGet 包)的 API 控制器——但是,我正在启动另一个项目,它只是不想遵循路由我安排好了.
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.
控制器:
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
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:
Global.asax.cs:
//注意:有关启用 IIS6 或 IIS7 经典模式的说明,//访问 http://go.microsoft.com/?LinkId=9394801
// 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);
}
}
当我尝试导航到/api/q/users 时 - 我收到 404 not found 错误.如果我将路由更改为/api/blazr/users" - 我会收到关于多个操作的错误,并且无法确定要执行的操作.
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!)
谢谢
编辑
routes.axd:
routes.axd:
api/{controller}/{id}
{resource}.axd/{*pathInfo}
{controller}/{action}/{id}
推荐答案
您不仅需要在 App_StartRouteConfig.cs
routes.MapMvcAttributeRoutes()
中调用routes.MapMvcAttributeRoutes()
/code> 文件,它必须出现在定义你的默认路由之前!我在忽略 {resource}.axd{*pathInfo}
之前和之后添加它.刚刚发现试图让属性路由与我的 MVC 5 网站正常工作.
Not only do you have to have the call to routes.MapMvcAttributeRoutes()
in your App_StartRouteConfig.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屋!