本文介绍了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:

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

}

推荐答案

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

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.



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

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

这篇关于C#MVC 4 ControllerName属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:47