Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 3使用自定义消息进行属性重定向_C#_Asp.net Mvc_Asp.net Mvc 3_Authorization - Fatal编程技术网

C# MVC 3使用自定义消息进行属性重定向

C# MVC 3使用自定义消息进行属性重定向,c#,asp.net-mvc,asp.net-mvc-3,authorization,C#,Asp.net Mvc,Asp.net Mvc 3,Authorization,如何创建自定义的authorized属性,以字符串参数的形式指定消息,然后将其传递到登录页面 例如,理想情况下,这样做会很酷: [Authorize(Message = "Access to the blah blah function requires login. Please login or create an account")] public ActionResult SomeAction() { return View(); } 然后,在登录操作中,我可以执行以下操作:

如何创建自定义的authorized属性,以字符串参数的形式指定消息,然后将其传递到登录页面

例如,理想情况下,这样做会很酷:

[Authorize(Message = "Access to the blah blah function requires login. Please login or create an account")]
public ActionResult SomeAction()
{
    return View();
}
然后,在登录操作中,我可以执行以下操作:

public ActionResult Login(string message = "")
{
    ViewData.Message = message;

    return View();
}
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    public string Message { get; set; }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var result = new ViewResult();
        result.ViewName = "Login.cshtml";        //this can be a property you don't have to hard code it
        result.MasterName = "_Layout.cshtml";    //this can also be a property
        result.ViewBag.Message = this.Message;
        filterContext.Result = result;
    }
最后,在我看来,我可以做到这一点:

@if (!String.IsNullOrEmpty(ViewData.Message))
{
    <div class="message">@ViewData.Message</div>
}

<form> blah blah </form>
@if(!String.IsNullOrEmpty(ViewData.Message))
{
@ViewData.Message
}
废话

基本上,我希望将自定义消息传递到登录页面,以便显示特定于用户在特定时间尝试访问的内容的消息。

您可以尝试以下操作:

public ActionResult Login(string message = "")
{
    ViewData.Message = message;

    return View();
}
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    public string Message { get; set; }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var result = new ViewResult();
        result.ViewName = "Login.cshtml";        //this can be a property you don't have to hard code it
        result.MasterName = "_Layout.cshtml";    //this can also be a property
        result.ViewBag.Message = this.Message;
        filterContext.Result = result;
    }
用法:

    [CustomAuthorize(Message = "You are not authorized.")]
    public ActionResult Index()
    {
        return View();
    }
web.config

 <authentication mode="Forms">
       <forms name="SqlAuthCookie"
           loginUrl="~/Account/LogOnYouHavenotRight" 
           timeout="2880"     />
 </authentication>
两个视图中:

Html.BeginForm("LogOn", "Account" )

如果我想重定向到一个特定的视图/控制器/路由参数,该怎么办?@ClayKaboom看到这篇博文,你可能不想在ViewName属性中包含“.cshtml”。当我这样做时,它寻找Login.cshtml.cshtml。这只是整个答案的一部分,但如果你阅读其他答案,你可能会足够聪明,可以将它们相加为+1。。。。