看微软的mvc3的登录模板示例里有[AllowAnonymous]的标记.我自己写就提示未能找到引用和命名空间,我看示例里的引用我都有了.
[AllowAnonymous] public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); }
我自己写[AllowAnonymous]就报错,请问是什么原因?要怎么做才能正常的添加?
还有用sql创建表的时候怎么把列的注释也加进去?
[AllowAnonymous]在ASP.NET MVC 4中内置了,但在ASP.NET MVC 3中需要自己实现一下,比如:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public class AllowAnonymousAttribute : Attribute { }
参考文章:Securing your ASP.NET MVC 3 application
为什么在微软的mvc3模板中,没有看到AllowAnonymous的实现,直接打就有了?
@棋怜: 你选中AllowAnonymous,F12看一下它在哪里定义的
@dudu: 转到定义是下面这样.自动定义的好像.我是不是需要自己手动的写一个这东西?
#region 程序集 System.Web.Mvc.dll, v4.0.0.0 // E:\Object\MvcDemo\packages\Microsoft.AspNet.Mvc.4.0.20505.0\lib\net40\System.Web.Mvc.dll #endregion using System; namespace System.Web.Mvc { // 摘要: // Represents an attribute that marks controllers and actions to skip the System.Web.Mvc.AuthorizeAttribute // during authorization. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public sealed class AllowAnonymousAttribute : Attribute { // 摘要: // Initializes a new instance of the System.Web.Mvc.AllowAnonymousAttribute // class. public AllowAnonymousAttribute(); } }
@棋怜:
程序集 System.Web.Mvc.dll, v4.0.0.0
这里是ASP.NET MVC 4.0,微软的示例程序应该是基于ASP.NET MVC 4.0的。
@dudu: 好的谢谢.