ASP MVC ActionLink causes invalid URL - asp.net

Using the standard route pattern
{controller}/{action}/{code}
and then a standard ActionLink in my view
<%: ActionLink("Details", "Details", new { code = item.Code }) %>
When the user has entered "N/A" as their code I see the following URL
http://localhost/AbsenceCode/Details/N/A
When I expect to see this
http://locahost/AbsenceCode/Details/N%2FA
Also, if the user has "A:B" as their code I see the correct URL (colon is url escaped) but I get a "400 bad request" from the server.
Does anyone have solutions to these problems? I cannot force the customer to use URL safe characters only.

Try
<%: Html.ActionLink("Details", "Details", new { code = Url.Encode(item.Code) }) &>

I've come up with a solution, source code is available here:
https://mrpmorris.blogspot.com/2012/08/asp-mvc-encoding-route-values.html

Related

How can i solve these ASP redirect error?

I put these sentence on the top of index.asp file
<% RESPONSE.REDIRECT "URL" %>
I've faced error
I also tried to put these sentence on the top of index.asp file
#include virtual=".\ssl.asp"
as remark
and made ssl.asp file with these datas
Your question is not clear. It may cover two realities :
Using <% RESPONSE.REDIRECT "URL" %> won't work because "URL" is a string not containing a valid url.
If you want to redirect to a valid url that is defined in a String variable named URL, then you would have to use something like :
Dim URL
URL = "https://somwhere.yourwebsite.com/yourpage.asp"
RESPONSE.REDIRECT URL

ASP Classic Friendly URLs via Custom Error Pages

I want to beautify my URLs and make them SEO-friendly.
I've encountered numerous methods doing so including several IIS modules, and creating a custom error page with a response.transfer redirect to "original" url (http://evolt.org/search_engine_friendly_urls_with_iis_and_classic_asp/)
I've actually implemented the last method and it works well, but I want to ask:
Isn't it problematic SEO-wise to lead the user/search-engine to a semi error page?
I mean, doesn't it have a bad effect on search engines?
Thanks!
P.S. The code on the custom error page looks like this
<%
strQuerystring = Mid(Request.ServerVariables("QUERY_STRING"),12)
aParameters = Split(strQuerystring,"/")
On Error Resume next
Server.Transfer("/"&aParameters(1) & ".asp")
If Err Then
Response.Status = "404 Not Found"
Server.Transfer("/error/real-404-page.asp")
End If
%>
As long as the page you're transferring the request to will return an HTTP 200 OK status, the search engine crawler has no way to know that internally you used this 'hack' to support friendly URLs.

UserManager.GenerateEmailConfirmationToken returning tokens unsuitable for URL transmission

I have a ASP.NET MVC 5 website, and I'm implementing an email confirmation process based on the template from Microsoft.
While I'm composing the email body text, first I construct the URL a user will use to "click to verify your address".
To generate the security token I call:
UserManager.GenerateEmailConfirmationTokenAsync(user.Id)
This produces a code such as:
pporPNj6KzdZ3BYG8vQsKJu3dPJMwGgh+ZEGhCNnf9X6F0AS0f6qCowOQwQNfpYkl14bgEsmyPTKya5H6N4n2na2n5PgO+wpoihXxQTA7G8pK/lUYskX3jy2iA/ZM8m4Vm0prTyUuhMgfDlV+wkbR336FBRIAbKJDwOWvHHbJBDQ21gW93hyzca0li66aI1H
Obviously, this wouldn't be valid in a URL, but even URL encoding won't solve IIS's hate of such a URL.
HTTP Error 404.11 - Not Found
The request filtering module is configured to deny a request that contains a double escape sequence.
In my UserManager implementation, I'm using the tutorial boilerplate code for a TokenProvider.
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<SiteUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
How can I make these generated tokens a bit more URL friendly? What did potentially change that would prevent the ASP.NET's tutorial code not work?
It turns out that this token will get mangled by the built in class "UrlHelper" in a MVC controller, or Url in a WebAPI controller IF the target route lists these variables as part of the path, rather than the GET vars of the URL.
Eg: this call, creates a relative URL for the site route called "ConfirmEmail" and fills in the blanks
Url.Route("ConfirmEmail", new { userId = user.Id, code = code });
Before my route was:
[Route("register-email/{code}/{userId}", Name = "ConfirmEmail")]
Changing this to:
[Route("register-email", Name = "ConfirmEmail")]
Generates valid URLS that IIS can chew through. When these are not specified, they get appended after a ? mark as normal GET vars. No idea why IIS is picky like that, but there's the solution.

Passing a variable in ASP using reponse redirect

I'm trying to pass a variable using Response.Redirect I have a page that I'm processing the info on which contains:
divrec = request.QueryString("div")
divstring = "divisions.asp?"&divrec
Response.Redirect divstring
But when I try to retrieve the information in another page by using
<% divrec = request.QueryString("div")
%>
<% =divrec %>
The variable/string does not display
i think you missed the querystring parameter in your divstring variable.
Try this:
divstring = "divisions.asp?div="&divrec
You should be able to access the parameter in the receiving page now.
What you could do is get the parameter from the url.
So if you redirected the user to www.mysite.com/divisions.asp?something
You could parse it up to the ? and get everything after it.
Try looking at http://www.powerasp.net/content/new/get-current-page-url.asp
Also this page might help you out
how to pass values from one page to other?

jQuery ajax call containing script-tag in data

I read some values from text boxes and send them via jQuerys post method to an server. If the user enters text containing something like "bla bla", the call fails. The data looks like this in that case:
var data = { myKey: 'bla <script> bla' };
And I send it to the server like this:
$.post(targetUrl, data, function(x) {...});
On the server side (an Asp.Net web form) it looks like the call never reaches the server. Any hint how to solve that? If there's a convenient function which cleans data from bad tags, that would be fine too.
Have you desactivate the validate request of your aspx page?
add this in your page declaration: validateRequest="false"
To strip tags using a jQuery function:
jQuery.fn.stripTags = function() {
return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') );
};
Do you receive a page_load in ASP.NET? If yes, isn't there anything in Request.Params?
I would suggest escaping your values client side using the javascript escape function as shown below
var data = { myKey: escape('bla <script> bla') };
Once you have done that, you can retrieve the correct value on the server side using the following (.Net Code)
HttpUtility.UrlDecode(param_value_to_decode)
I tested this and the correct value is being passed correctly to the server via the post request.
Hope this helps.
Additional Info : I forgot to mention the cause of the error. When inspecting the request using firebug, it returns a "500 Internal Server Error - A potentially dangerous Request.Form value was detected from...". This is a built in protection mechanism from asp.net to protect against script injection. The following page directive ValidateRequest="false" did not solve the problem as expected (Works in traditional WebForms). It might be something specific to the Mvc platform, not to sure. The above solution does work, so just use that.
Regards
G

Resources