C # MVC 4 ControllerName Property

I am 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 entire controller.

I could not find any information about this attribute, so how can I do this? Also, will I need to add a new MapRoute to handle it?

EDIT:

For example, I would like to forward the following URL:

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

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

}

+6
source share
3 answers

I don’t know if it will answer your question, but even if it were, why would you do that in the world? Why not make a standard path: call the controller class anything (as long as it makes sense) and have you called this framework for you? Why create two conditional naming conventions and match them together? I do not see a single win, but a few minuses. Such as - it is harder to read, harder to maintain, harder to understand, it is also insignificant, but still, stress on performance ...

Update

Please study this publication, I think they have solved the problem you are talking about.

Please let me know if this helps you.

-1
source

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 containing a β€œMy-Friendly-Name” for the controller will use your new route.

+6
source

Checkout this post , especially where it talks about formatting the route.

+2
source

Source: https://habr.com/ru/post/925971/


All Articles