在实体框架中的MVC3中的按钮单击事件上的注销功能

时间:2013-08-10 10:11:51

标签: asp.net-mvc-3 authentication url-action

我正在使用带剃刀的mvc3 我有一个用户注销按钮,我使用以下代码 共享/ _layout.cshtml

      //button
     <div class="button"><a href="#" id="Logout" title="Logout">


          <div class="Logout"></div>Logout</a></div>
    <script>
          $("#Logout").bind("click", function () {
window.location.href = '@Url.Action("LogOn", "Account")'; 
});
    </script>

accountcontroller.cs

 public ActionResult Logout()
        {
          FormsAuthentication.SignOut();
          return RedirectToAction("LogOn");
        }

这会将我引导至错误页面

http://localhost:XXXX/%3C%=%20Url.Action(%22Logout%22,%20%22Account%22)%20%%3E

Server Error in '/' Application.

HTTP Error 400 - Bad Request.

Version Information: ASP.NET Development Server 11.0.0.0
谁能告诉我哪里出错了? 请帮助按钮点击事件以进行注销更改。

更新:

 http://localhost:8436/&#47;Account&#47;Logout


enter code here
Server Error in '/' Application.

A potentially dangerous Request.Path value was detected from the client (&).

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: A potentially dangerous Request.Path value was detected from the client (&).

Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 


    [HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (&).]
       System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +9673044
       System.Web.ValidateRequestExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +35
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

2 个答案:

答案 0 :(得分:1)

Razor语法使用和@字符而不是<%,所以使用它并且一切都应该正常工作

$("#Logout").bind("click", function () { 
    window.location.href = '@Url.Action("LogOn", "Account")'; // assuming this is the correct action you want to use for the logout button
});

您可能还想从jQuery $("#Logout").bind切换到$("#Logout").click

答案 1 :(得分:1)

为什么不尝试ajax jquery调用

<a href="#" id="Logout" onclick=="return LogOut();" title="Logout">

function LogOut() {
$.ajax({
                            cache:true,
                            type: "POST",
                            url: "@(Url.Action("LogOut", "Account"))",
                            success: function (data) {
                                                window.location.reload();
                                             },
                            error:function (xhr, ajaxOptions, thrownError){

                                              }, 
                            complete: function() {   } 

                           }); // end ajax code
                   }

这可能看起来很冗长但应该有效!

相关问题