C#MVC 4 ControllerName属性

编程入门 行业动态 更新时间:2024-10-11 17:25:19
本文介绍了C#MVC 4 ControllerName属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的工作提供了我的MVC 4控制器友好的名字,我想要做的事,如 [ActionName =我的好记的名称] 的风格,但对于整个控制器。

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?

编辑:

例如,我想,将以下网址:

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

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

}

推荐答案

我不知道是否有回答你的问题,但即使有之一,为什么在世界上,你会做这样的事?为什么不标准的方法:调用控制器类任何你喜欢的(只要它是有意义),并经为你的框架叫什么名字?为什么要创建两个人工命名约定以及它们之间的映射?我看到的不是一个单一的增益,但多个缺点。如 - 其更难阅读,难以维持,更难理解,它也是微不足道的,但尽管如此,对性能有应变...

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 ...

更新

请看看这个帖子,我觉得他们确实解决了你在谈论这个问题。

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

  • How以实现在ASP.NET MVC动态控制器和操作方法?

请让我知道,如果它是任何帮助你。

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

更多推荐

C#MVC 4 ControllerName属性

本文发布于:2023-10-12 04:05:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   MVC   ControllerName

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!