Alex Rivera | Logout

C# MVC 4 ControllerName attribute

Asked 2012-09-22T01:31:55.817
8

I'm working on providing friendly names for my MVC 4 controllers and I want to do something like the [ActionName="My-Friendly-Name"] style, but for the whole controller.

I couldn't find any information about such an attribute, so how would I go about doing that? Also, will I need to add a new MapRoute to handle it?

EDIT:

For example, I'd like to route the following url:

 http://mysite.com/my-reports/Details/5

be routed to the following controller:

[ControllerClass="my-reports"]  // This attribute is made up.  I'd like to know how to make this functionality
public class ReportsController : Controller
{
    //
    // GET: /Reports/

    public ActionResult Index()
    {
        return View();
    }

    public ViewResult Details(int id)
    {
        Report report = db.Reports.Single(g => g.Id == id);
        return View(report);
    }

    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(Report item)
    {
        try
        {
            if (ModelState.IsValid)
            {
                item.Id = Guid.NewGuid();
                _context.Reports.AddObject(item);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(item);
        }
        catch (Exception)
        {
            return View(item);
        }
    }

}

Edit
Report

1 Answer

2

Checkout this post, particularly where it talks about Route formatting.

answered 2012-09-24T02:01:14.120

Your Answer