Getting current action name from partial page
I'm using a custom HtmlHelper in a partial page.
Example:
/Home/Index - Is the Main Page with Index View
/Home/Partial - Is the Partial Action with Partial - A Partial View
In the Index view:
Html.RenderAction("Part开发者_C百科ial");
In the Partial View:
I'm using a custom htmlhelper in the that htmlhelper I need to get the url of where the request is from?
Let Say It should be like "/Home/Partial"
How can I get this in my htmlhelper method
There are a couple of ways you could do this depending on what you want. Your HtmlHelper's ViewContext property will have just about everything you need about the particular request: HttpContext, RequestContext, RouteData, TempData, ViewData, etc.
To get the current path of the request, try helper.ViewContext.HttpContext.Request.Path
. This would return the actual request path, likely "/" or "/home/index" if the path was explicit in the URL.
I'm not sure why you looking to get "/home/partial". Since this is a partial view the request would always come from somewhere else, eg. "/home/index".
Regardless, you can check the RouteData and obtain the (partial) action and controller, among other route values:
public static string TestHelper(this HtmlHelper helper)
{
var controller = helper.ViewContext.RouteData.Values["controller"].ToString();
var action = helper.ViewContext.RouteData.Values["action"].ToString();
return controller + "/" + action;
}
If called in your Index view (Index.aspx) it would return "Home/Index".
If called in your Partial view (Partial.aspx) it would return "Home/Partial".
Hope that helps.
精彩评论