ASP.NET MVC 3 中是否有用于类类型声明的 ActionNameAttribute?

发布于 2024-11-17 06:48:20 字数 625 浏览 0 评论 0原文

无法想象这个问题还没有被问过......尽管我找不到它。

事情是这样的:我在 ASP.NET MVC 3 中的操作方法中大量使用了 ActionNameAttribute。就像这样:

[ActionName("confirm")]
[HttpGet]
public ActionResult Confirm(string stuffTitle)
{
   ActionResult resultView = CreateSomeStuff(stuffTitle);
   return resultView;
}

但是,如何才能对类实现相同的效果呢?像这样:

[ActionName("personal")]
public class AccountController : Controller

使用 ActionNameAttribute 只能在方法声明上使用。但我想在类类型声明上应该可以实现。也许还有一个我还没听说过的属性?还有其他解决方法吗?

有人有想法吗?提前致谢!!

Can't imagine this question hasn't been asked yet... although, I can't find it.

So here it goes: I've used the ActionNameAttribute a lot on my action methods in ASP.NET MVC 3. Like this:

[ActionName("confirm")]
[HttpGet]
public ActionResult Confirm(string stuffTitle)
{
   ActionResult resultView = CreateSomeStuff(stuffTitle);
   return resultView;
}

But, how can I accomplish the same for classes? Like this:

[ActionName("personal")]
public class AccountController : Controller

With the ActionNameAttribute it's only possible on method declartions. But I guess it should somehow be possible on class type declarations. Maybe another attribute I haven't heard of yet? Some other workaround?

Anyone an idea? Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女的英雄梦 2024-11-24 06:48:20

我真的很难理解你在追求什么。您想更改控制器的名称或其他名称吗?如果是这样,请使用路由:

routes.MapRoute(
    "FooRoute",
    "foo/{action}/{action}/{id}",
    new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);

现在,所有对 foo/someaction 的请求都将被路由到 Account 控制器的相应操作。

I really have hard time understanding what are you after. You want to change the name of your controller or something? If so use a route:

routes.MapRoute(
    "FooRoute",
    "foo/{action}/{action}/{id}",
    new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);

Now all requests to foo/someaction will be routed to the corresponding action of the Account controller.

总以为 2024-11-24 06:48:20

MVC 控制器包含可以映射到操作的方法。那么正如达林所说,它在课堂上能达到什么目的呢?说这个类有一个“列表”操作,是没有帮助的。您还必须说出哪种方法,不是吗?

我在 Phil Haack 的博客 上找到了这篇文章http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx。如果您已经阅读过它,我提前表示歉意。我认为他所说的以及由此产生的一些讨论可能会对您有所帮助。

MVC Controllers contain methods which can map to Actions. So as Darin says, what purpose would it achieve on a class? To say this class has a 'list' action, wouldn't help. You'd have to say which method as well, wouldn't you?

I found this article on Phil Haack's blog, http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx . I apologise in advance if you've already read it. I think what he says and some of the resulting discussion might help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文