3
I need to create an Htmlhelper (MVC 4, C#) that identifies and tells what is the Area, Controller and Action of a View. I’m not getting anything.
How can this be done?
Is there a function or method that already does this?
3
I need to create an Htmlhelper (MVC 4, C#) that identifies and tells what is the Area, Controller and Action of a View. I’m not getting anything.
How can this be done?
Is there a function or method that already does this?
3
In a basic MVC project you can do the following:
public class HomeController : Controller
{
public ActionResult Index()
{
var e = this.RouteData.Values;
string controllerName = (string)e["controller"];
string actionName = (string)e["action"];
return View();
}
}
If you want to take these values in a generic way (especially in filters), you can access Routedata through the Onactionexecuting method (there are others that provide this as well):
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var routeData = filterContext.RouteData; // aqui também
base.OnActionExecuting(filterContext);
}
Complement: Collecting data through an extension
Create a class within the ~/Extensions/ folder named Locationhelper.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebTest.Extensions
{
/// <summary>
/// Dados sobre a localização obtido da rota atual
/// </summary>
public class LocationData
{
public string ActionName { get; set; }
public string ControllerName { get; set; }
}
public static class LocationHelper
{
public static LocationData GetLocationData<TModel>(this WebViewPage<TModel> page)
{
// TODO: validate page, ViewContext, RouteData, Values
// for:
// not null, contain values
return new LocationData()
{
ActionName = (string)page.ViewContext.RouteData.Values["action"],
ControllerName = (string)page.ViewContext.RouteData.Values["controller"]
// TODO: get area name
};
}
}
}
refer to it in the View and use the static method as follows:
@using WebTest.Extensions
<div>
<label>Action Name: </label>
<output>@this.GetLocationData().ActionName</output>
</div>
<div>
<label>Controller Name: </label>
<output>@this.GetLocationData().ControllerName</output>
</div>
After this run the page, note the results:
There are some things to be done:
Extra points:
App_Code
(can do this in the context menu of the project) and after that with the right button in this folder you can add a Helper (it creates a base code for you), the way to use is absolutely the same, the difference is that this helper is more geared to the syntax Razor.Browser other questions tagged c# asp.net-mvc-4 controller
You are not signed in. Login or sign up in order to post.
I forgot the detail of Htmlhelper... inside the View call the helper passing @Viewcontext.Routedata (same thing)
– Leonardo Bosquett
You could show me how to use this helper, I’m starting now at MVC
– PHPlima
added as a complement to the reply, I hope it helps :)
– Leonardo Bosquett
Thank you very much :D
– PHPlima
How to identify the area?
– Danilo Pádua