Given a particular URL, how do I find out for sure which controller action it is being routed to (perhaps in the context of a large application with many controllers and a complex route registry)?
给定一个特定的URL,我如何确定它被路由到哪个控制器操作(可能在具有许多控制器和复杂路由注册表的大型应用程序的上下文中)?
I'm not asking how to configure routes.
我不是问如何配置路由。
6 个解决方案
#1
16
Something like this for controller:
对于控制器来说这样的东西:
string controller = RouteData.GetRequiredString("controller");
And for action:
并采取行动:
string action = RouteData.GetRequiredString("action");
For example you can use it in your base controller class:
例如,您可以在基本控制器类中使用它:
public class YouControllerBase: Controller
{
protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
string controller = requestContext.RouteData.GetRequiredString("controller");
string action = requestContext.RouteData.GetRequiredString("action");
}
}
Or use it in global.asax:
或者在global.asax中使用它:
protected void Application_BeginRequest(object sender, EventArgs e)
{
RouteData routeData = RouteTable.Routes.GetRouteData(
new HttpContextWrapper(HttpContext.Current));
var action = routeData.GetRequiredString("action");
}
#2
9
You could try this ASP.NET Routing Debugger:
您可以尝试此ASP.NET路由调试器:
alt text http://haacked.com/images/haacked_com/WindowsLiveWriter/UrlRoutingDebugger_EEBA/route-debugger-screenshot.png
#3
2
pete,
皮特,
in code, you can use an actionfilter to determine whats going on:
在代码中,您可以使用actionfilter来确定发生的事情:
public class AddUrlInfoToSessionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
// where we are now - do something with the vars in real app
var currentActionName = filterContext.ActionDescriptor.ActionName;
var currentControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var currentRouteData = filterContext.RouteData;
var currentUrlInfo = new UrlHelper(filterContext.RequestContext);
string url = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, currentRouteData.Values).VirtualPath;
}
}
}
and then decorate each controller that your interested in as below (or put it onto a basecontroller):
然后装饰你感兴趣的每个控制器如下(或把它放到一个basecontroller上):
[HandleError]
[AddUrlInfoToSessionAttribute]
public class HomeController : Controller
{
// controller stuff
}
[AddUrlInfoToSession]
public abstract class BaseController : Controller
{
}
hope this helps
希望这可以帮助
jim
吉姆
EDIT: just tidied the example up a bit by adding the following to the filter method:
编辑:通过在filter方法中添加以下内容,稍微整理了一下示例:
string url = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, currentRouteData.Values).VirtualPath;
string url = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext,currentRouteData.Values).VirtualPath;
#4
1
Two simple steps:
两个简单的步骤:
- Add 2 reference in your project:
MvcFakes.dll,RouteDebugger.dll
- 在项目中添加2个引用:MvcFakes.dll,RouteDebugger.dll
- Entering the following URL:
/RouteDebugger
- 输入以下URL:/ RouteDebugger
The 2 dlls are contained here
这里包含2个dll
Chapter09Code.zip\Code\CS\MvcApplication1\MvcApplication1\bin
Chapter09Code.zip \代码\ CS \ MvcApplication1 \ MvcApplication1 \ BIN
#5
0
It depends on Routes that are registered to resolve the URL
它取决于注册用于解析URL的路由
The default conventions is as follows :
默认约定如下:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
Index action will be called on HomeController.
将在HomeController上调用索引操作。
#6
0
The Controller and Action that are used are determined by the registered routes. Look in your Global.ascx.cs
使用的控制器和操作由注册的路由确定。查看Global.ascx.cs
Here is the default route mapping that comes as standard with a new MVC app. So if your url is http://yourdomain/SomeSiteSection/SomeThing/ then MVC will look for a controller called SomeSiteSectionController and an Action called SomeThing.
这是默认路由映射,它是新MVC应用程序的标准配置。因此,如果您的网址是http:// yourdomain / SomeSiteSection / SomeThing /,那么MVC将查找名为SomeSiteSectionController的控制器和名为SomeThing的Action。
public class MvcApplication : System.Web.HttpApplication
{
//...
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
#1
16
Something like this for controller:
对于控制器来说这样的东西:
string controller = RouteData.GetRequiredString("controller");
And for action:
并采取行动:
string action = RouteData.GetRequiredString("action");
For example you can use it in your base controller class:
例如,您可以在基本控制器类中使用它:
public class YouControllerBase: Controller
{
protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
string controller = requestContext.RouteData.GetRequiredString("controller");
string action = requestContext.RouteData.GetRequiredString("action");
}
}
Or use it in global.asax:
或者在global.asax中使用它:
protected void Application_BeginRequest(object sender, EventArgs e)
{
RouteData routeData = RouteTable.Routes.GetRouteData(
new HttpContextWrapper(HttpContext.Current));
var action = routeData.GetRequiredString("action");
}
#2
9
You could try this ASP.NET Routing Debugger:
您可以尝试此ASP.NET路由调试器:
alt text http://haacked.com/images/haacked_com/WindowsLiveWriter/UrlRoutingDebugger_EEBA/route-debugger-screenshot.png
#3
2
pete,
皮特,
in code, you can use an actionfilter to determine whats going on:
在代码中,您可以使用actionfilter来确定发生的事情:
public class AddUrlInfoToSessionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
// where we are now - do something with the vars in real app
var currentActionName = filterContext.ActionDescriptor.ActionName;
var currentControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var currentRouteData = filterContext.RouteData;
var currentUrlInfo = new UrlHelper(filterContext.RequestContext);
string url = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, currentRouteData.Values).VirtualPath;
}
}
}
and then decorate each controller that your interested in as below (or put it onto a basecontroller):
然后装饰你感兴趣的每个控制器如下(或把它放到一个basecontroller上):
[HandleError]
[AddUrlInfoToSessionAttribute]
public class HomeController : Controller
{
// controller stuff
}
[AddUrlInfoToSession]
public abstract class BaseController : Controller
{
}
hope this helps
希望这可以帮助
jim
吉姆
EDIT: just tidied the example up a bit by adding the following to the filter method:
编辑:通过在filter方法中添加以下内容,稍微整理了一下示例:
string url = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, currentRouteData.Values).VirtualPath;
string url = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext,currentRouteData.Values).VirtualPath;
#4
1
Two simple steps:
两个简单的步骤:
- Add 2 reference in your project:
MvcFakes.dll,RouteDebugger.dll
- 在项目中添加2个引用:MvcFakes.dll,RouteDebugger.dll
- Entering the following URL:
/RouteDebugger
- 输入以下URL:/ RouteDebugger
The 2 dlls are contained here
这里包含2个dll
Chapter09Code.zip\Code\CS\MvcApplication1\MvcApplication1\bin
Chapter09Code.zip \代码\ CS \ MvcApplication1 \ MvcApplication1 \ BIN
#5
0
It depends on Routes that are registered to resolve the URL
它取决于注册用于解析URL的路由
The default conventions is as follows :
默认约定如下:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
Index action will be called on HomeController.
将在HomeController上调用索引操作。
#6
0
The Controller and Action that are used are determined by the registered routes. Look in your Global.ascx.cs
使用的控制器和操作由注册的路由确定。查看Global.ascx.cs
Here is the default route mapping that comes as standard with a new MVC app. So if your url is http://yourdomain/SomeSiteSection/SomeThing/ then MVC will look for a controller called SomeSiteSectionController and an Action called SomeThing.
这是默认路由映射,它是新MVC应用程序的标准配置。因此,如果您的网址是http:// yourdomain / SomeSiteSection / SomeThing /,那么MVC将查找名为SomeSiteSectionController的控制器和名为SomeThing的Action。
public class MvcApplication : System.Web.HttpApplication
{
//...
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}