控制器名是从类名派生的吗? [英] Is the controller name derived from the class name?

查看:97
本文介绍了控制器名是从类名派生的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个新手问题...

This is a newbie question...

我正在查看默认的asp.net mvc3项目,注意到有一个控制器:

I am looking at the default asp.net mvc3 project and noticed that there is a controller called:

public class AccountController : Controller


$ b b

我查看整个代码,找不到指定AccountController映射到/ Account /的URL的地方。

I looked throughout the code and couldn't find a place that specified AccountController maps to /Account/ for the URL.

我发现你可以更改使用 Global.asax 中的 routes.MapRoute(..)路由,但我仍然不知道它们在哪里指定AccountController映射到/ Account /.

I discovered that you can change the routing using routes.MapRoute(..) in the Global.asax, but I still don't know where they specified that AccountController maps to /Account/.

如果假定从类名称,那么是否意味着所有控制器类都必须命名为xxxxxController?

If it is assumed from the class name, then does that mean all controller classes have to be named xxxxxController?

推荐答案

是的,所有控制器都需要遵循命令约定结束Controller

Yes you are right, all controllers need to follow the naming convention of an ending "Controller".

请参阅 ControllerName 在CodePlex上的ASP.NET MVC代码中的属性:

See the ControllerName property in the ASP.NET MVC code on CodePlex:

public virtual string ControllerName {
    get {
        string typeName = ControllerType.Name;
        if (typeName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
        {
            return typeName.Substring(0, typeName.Length - "Controller".Length);
        }
        return typeName;
    }
}

无论如何,你可以改变命名约定自己的控制器工厂。

Anyhow, you could change the naming convention by using your own controller factory.

希望有帮助。

这篇关于控制器名是从类名派生的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆