1
votes

I use navigation like this:

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

But before the user is navigated I handle the event in 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:

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() +9807692
System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +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?

1

1 Answers

0
votes

if the last thing you do in your event handler is go to the exact href, then remove:-

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

and make sure you DONT preventDefault or return false;.

The browser will redirect using the href off the anchor after the click event as normal.

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.