简体   繁体   English

在ASP.NET MVC应用程序中使用href属性进行重定向不起作用

[英]Redirecting using href attributes in asp.net mvc application not working

I use navigation like this: 我使用这样的导航:

<a href="/MyMessages/?messageId=1"> First message </>

But before the user is navigated I handle the event in JavaScript: 但是在导航用户之前,我使用JavaScript处理事件:

$('a').on('click', function (event) {
    event.preventDefault();
    // Do stuff
    // Navigate:
    location.pathname = $(this).attr('href'); // href is /MyMessages/?messageId=1
});

But it gets encoded in browser, and the address is: 但是它是在浏览器中编码的,地址是:

localhost:52500/MyMessages/%3FmessageId=1

and the mvc routing fails: 并且MVC路由失败:

Server Error in '/' Application. '/'应用程序中的服务器错误。

A potentially dangerous Request.Path value was detected from the client (?). 从客户端(?)检测到潜在的危险Request.Path值。

Description: An unhandled exception occurred during the execution of the current web request. 描述:执行当前Web请求期间发生未处理的异常。 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 (?). 异常详细信息:System.Web.HttpException:从客户端(?)检测到潜在危险的Request.Path值。

Source Error: 来源错误:

An unhandled exception was generated during the execution of the current web request. 在执行当前Web请求期间生成了未处理的异常。 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 (?).] [HttpException(0x80004005):从客户端(?)检测到潜在的Request.Path值。
System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +9807692 System.Web.HttpRequest.ValidateInputIfRequiredByConfig()+9807692
System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +53 System.Web.PipelineStepManager.ValidateHelper(HttpContext上下文)+53

But when I just navigate by deeplinking like 但是当我像这样通过深层链接导航时

localhost:52500/MyMessages/?messageId=1

everything works. 一切正常。

How to avoid my browser encoding my url and preventing the error I get? 如何避免浏览器对URL进行编码并防止出现错误?

if the last thing you do in your event handler is go to the exact href, then remove:- 如果您在事件处理程序中所做的最后一件事是转到确切的href,则删除:-

location.pathname = $(this).attr('href');

and make sure you DONT preventDefault or return false; 并确保您不要执行preventDefaultreturn false; .

The browser will redirect using the href off the anchor after the click event as normal. 在正常的click事件之后,浏览器将使用href离开anchor进行重定向。

User can also choose to stay on the page (popup has yes/no buttons, after preventing default if its dirty), and then if he chooses to navigate again, the same would happen 用户还可以选择停留在页面上(弹出窗口具有是/否按钮,如果默认设置为脏则在阻止默认设置之后),然后如果他选择再次导航,则同样会发生

try something like:- 尝试类似:

$('a').on('click', function (event) {
    if(dirty){
       event.preventDefault();
       $('#yes').attr('href', $(this).attr('href'));
       $('#popup').show();
    }
});

and create the #yes as an anchor. 并创建#yes作为锚点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM