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);
        }
    }

}

joe_coolish
  • 7,201
  • 13
  • 64
  • 111

4 Answers4

8

Add a custom route that will match your specific name:

routes.MapRoute(
    "MyCustomRoute",
    "My-Friendly-Name/{action}/{id}",
    new { controller = "ReportsController", action = "Index", id = "" }
);

Now every url that contains "My-Friendly-Name" for the controller will use your new route.

smirne
  • 220
  • 2
  • 8
2

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

Max Toro
  • 28,282
  • 11
  • 76
  • 114
0

using Microsoft.AspNetCore.Mvc;

[ControllerName("SomeOtherName")] // This attribute is not made up.
public class ReportsController : Controller { ...

martinp999
  • 419
  • 6
  • 10
-2

I don't know if there's answer to your question, but even if there were one, why in the world would you do such a thing? Why not do standard way : call the controller class anything you like (as long as it makes sense) and have it called by the framework for you? Why create two artificial naming conventions and map between them? I see not a single gain, but multiple cons. Such as - its harder to read, harder to maintain, harder to understand, it is also insignificant, but still, a strain on performance ...

Update

Please look into this posting, I think they did solve the problem that you're talking about.

Please let me know if it was of any help to you.

Community
  • 1
  • 1
Display Name
  • 4,672
  • 1
  • 33
  • 43
  • The reason is because some of the routing happens based on values stored in a database (So instead of having a url be `/Items/5` the url can be something like `/Media/Music/Somebody-to-Love`) and this has caused a few particular complications that the easiest way to fix would be to allow the same functionality as `[ActionName]` but for controllers. If there is no solution, I'll have to figure out another way. I'm sure there is, this was just my first thought :) – joe_coolish Sep 22 '12 at 04:36
  • Some of my customerrs prefer http://www.domain.com/produkter instead of http://www.domain.com/product. I don't want to have a ProdukterController and mix in Swedish in my code. I've solved it using AttributeRouting to make custom routes for all my controllers and actions. – Peter Hedberg Sep 30 '13 at 10:51
  • 1
    @DisplayName I don't understand your answer. The reason to want to do this is the same reason the `ActionName` attribute exists. It makes just as much sense to have a controller who's name has multiple words be split with '-' as it does for actions. my-controller maps to MyController – wired_in Feb 07 '14 at 21:48