Identify Area, Controller, and View Action

Asked

Viewed 1,236 times

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?

1 answer

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:

inserir a descrição da imagem aqui

There are some things to be done:

  • Validations in this extension, such as null values and also add Area return.

Extra points:

  • I didn’t make this example as a true ASP.NET Mvc 'Helper'... to create them you need to add the ASP.NET folder called 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.
  • I forgot the detail of Htmlhelper... inside the View call the helper passing @Viewcontext.Routedata (same thing)

  • You could show me how to use this helper, I’m starting now at MVC

  • added as a complement to the reply, I hope it helps :)

  • Thank you very much :D

  • How to identify the area?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.