Add Querystring from asp.net code behind not working - c#

Response.Redirect("~/CustomList.aspx?id="+Data.Item1+"&type="+Data.Item2);
I am adding this from Codebehind Asp.net
However it isn't working
Getting this Error
This page can't be displayed
•Make sure the web address http://localhost:51955 is correct.
•Look for the page with your search engine.
•Refresh the page in a few minutes.

Could you try something like this ?
string parameter1 = HttpUtility.UrlEncode(Data.Item1)
string parameter2 = HttpUtility.UrlEncode(Data.Item2)
Response.Redirect("~/CustomList.aspx?id="+parameter1 +"&type="+parameter2 );
And make sure that page is in the root of your project, is this page is in another project, you will need to change the port in the URL. Localhost:port/CustomList.aspx

Related

Change Page URL Dynamically

I am writing an e-commerce website using DotNetNuke, and I have ran into a problem. For example I have a module on a page that has a URL of mydomain/productType/product-pages. What I would like is to pass a query string to this page with the item number of product (lets say its name is bacon). And when page loads, I would like both the breadcrumbs and URL(at browser) to read mydomain/productType/product-pages/bacon. I have researched how to change the page title, meta description, and all that already and have tested and it works - but just cannot find a way to modify the URL. I don't even know if this is possible. My goal is to not create all the pages for products within DNN, because this will change over time. I'm pretty sure I can create a page within DNN each time page is passed the query string which is a possibility, and another possibility would be have my other module create the link like it should read(just no page created) and DNN would just land on product-pages just add the /bacon? But I would rather just spoof the URL if possible.
Any suggestions or help would be greatly appreciated, and Thanks for reading.
Below is code snippet for changing the title and description:
protected void Page_PreRender(object sender, EventArgs e)
{
string pageName = Request.QueryString["pageName"];
if (!string.IsNullOrEmpty(pageName))
{
Page.Title = pageName;
Page.MetaDescription = "Blah";
Page.MetaKeywords = "Stuff,more stuff";
var url = HttpContext.Current.Request.RawUrl;
//Page.ResolveUrl(url + "/" + pageName);//this didnt work
//below is another way compared to top
//DotNetNuke.Framework.CDefault myPage = new
DotNetNuke.Framework.CDefault();
//myPage = (CDefault)this.Page;
//myPage.Title = "This is the new title";
}
}
The best way to manipulate the URLs for your custom module is by building a Extension URL provider. But you need to reverse your thinking about the problem. You don't want an ugly URL with a querystring argument to change into another URL. Rather, you start with the desired URL path and you want that to resolve or be "written" as the ugly querystring URL under the covers. That's what a Extension URL provider does.
I have a tutorial on DNNHero.com that walks you through it.
https://www.dnnhero.com/video/introduction-and-url-rewriting-basics
Unfortunately, that video is behind a paywall. (IMHO, it is worth the cost even for this one tutorial and code.)
You can also check out the blog:
https://www.dnnsoftware.com/wiki/extension-url-providers#:~:text=An%20Extension%20URL%20Provider%20is,and%20logic%20to%20be%20implemented.

URL routing turning /pages/test.aspx into /test

I made a folder called 'pages' in my asp web forms project. In this folder I have a lot of pages:
test.aspx
hello.aspx
When I open these pages in a browser I get:
www.domain.com/pages/test.aspx
www.domain.com/pages/hello.aspx
This is normal, I know. But what if I want to delete the /pages in the url and just show (without .aspx):
www.domain.com/test*.aspx*
www.domain.com/hello*.aspx*
I can do this by manually adding a new route (in RegisterRoutes() method) for each page but is there a way to do this dynamicly?
I found this question but I don't know if I can use it for this problem.
WebForms custom / dynamic routing
Try something like this, not sure if that will do it. But you could always just put your pages in the root directory. I think this would work.
Source Link: http://msdn.microsoft.com/en-us/library/vstudio/cc668177(v=vs.100).aspx
routes.MapPageRoute("PageRoute","{page}", "~/pages/{page}.aspx");
Not sure if that's what you were looking for, this way you can just do something like:
Response.RedirectToRoute("PageRoute", new { page = "test" });

route.MapPageRoute to a page with an extension

In the global.asax of my website project (not MVC, not Web Application) MapPageRoute won't let me map to a page with an extension.
For example:
routes.MapPageRoute("GetMobilePassForAttendee", "Pass/Attendee/{attendeeId}", "~/GetMobilePass.aspx");
works as expected, but
routes.MapPageRoute("GetMobilePassForAttendee", "Pass/Attendee/{attendeeId}/pass.pkpass", "~/GetMobilePass.aspx");
returns a 404.
Anyone know why?
Perhaps I should be using URL rewriting, but all the material I've read has suggested I use routing instead.
Have you tested the "pass.pkpass" page separately, let say attendeeid=1 to see if Pass/Attendee/1/pass.pkpass is a working page without the routing?
Because .NET routing would not work in this case if this is a working page. It will skip the routing and load the actual "Pass/Attendee/1/pass.pkpass" page.
This worked for me:
routes.MapPageRoute("FederationMetadataRoute", "FederationMetadata/2007-06/{file}", "~/FederationMetadata/2007-06/FederationMetadata.aspx")
And then I just check the {file} parameter to make sure it equaled FederationMetadata.xml
So in your case, just set Pass/Attendee/{attendeeId}/pass.pkpass to Pass/Attendee/{attendeeId}/{pkpass}
and check {pkpass} to make sure it equals pass.pkpass on your page.

Forcing SSL (https) on a page by page basis

How can I set up my page load event in my code behind to redirect to an url that has an https prefix? I would need it to work with urls that have query strings attached too.
It's one thing to construct a link that goes straight to the https page, but I don't want the user to be able to manually change it to an http page.
Also I don't want to do it with javascript because it might be turned off.
I'm guessing a regular expression?
We mark our SSL Required pages with a special attribute ForceSslAttribute. Then we have a HttpModule that pulls down the current page's class and inspect it's attributes.
If the attribute is present on the page, it takes the exact url that was passed and changes the protocol from http to https then calls a redirect.
There's probably a bit simpler way of doing it, but that's how it's done for us.
Attribute:
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public sealed class ForceSslAttribute : Attribute
{
// Marker Attribute
}
Page Example (CodeBehind):
[ForceSsl]
public partial class User_Login : Page
{
//...
}
You can figure out the type of the page like this:
HttpContext.Current.CurrentHandler.GetType()
All Page's implement IHttpHandler and when you're visiting a page, it'll work.
The cool part about this method is you can mark anything that's an IHttpHandler and it'll force the redirect too :)
Add this at the top of your Page_Load
if (Request.ServerVariables["HTTPS"] != "ON")
{
Response.Redirect("https://" + Request["HTTP_HOST"] + Request.RawUrl);
}
I use the following in Global.asax Application_BeginRequest
If needsSSL <> Request.IsSecureConnection Then
If needsSSL Then
Response.Redirect(Uri.UriSchemeHttps + Uri.SchemeDelimiter + Request.Url.Host + Request.Url.PathAndQuery, True)
Else
Response.Redirect(Uri.UriSchemeHttp + Uri.SchemeDelimiter + Request.Url.Host + Request.Url.PathAndQuery, True)
End If
End If
There is an IIS7 module for URL rewriting. Very handy, but you need access to the IIS and it requires some time to learn how to write the rules. A simple http->https rule is a matter of seconds.
Just be careful because any rules you add will be stored in your web.config, so don't delete/override it or you will have to write them again.
Forcing SSL using ASP
To force SSL using ASP, follow these steps:
Click Start, click Run, type Notepad, and then click OK.
Paste the following code into a blank Notepad document. On the File menu, click Save As, and then save the following code in the root of your Web server as an include file named ForceSSL.inc:
<%
If Request.ServerVariables("SERVER_PORT")=80 Then
Dim strSecureURL
strSecureURL = "https://"
strSecureURL = strSecureURL & Request.ServerVariables("SERVER_NAME")
strSecureURL = strSecureURL & Request.ServerVariables("URL")
Response.Redirect strSecureURL
End If
%>
For each page that requires SSL, paste the following code at the top of the page to reference the include file from the previous step:
<%#Language="VBSCRIPT"%>
<!--#include virtual="/ForceSSL.inc"-->
When each page is browsed, the ASP code that is contained in the include file detects the port to determine if HTTP is used. If HTTP is used, the browser will be redirected to the same page by using HTTPS.

Can I post values to classic ASP page when debugging?

I have a classic ASP page that requires two values from a form. These values are posted to the ASP page from another pages form. I would like to pass these values to the ASP page without the need for a form for testing. Is this possible?
This is what the asp page looks like:
<%#LANGUAGE="JavaScript"%>
<%
var someID = new String( Request.Form("someID") );
var anotherID = new String( Request.Form("anotherID") );
%>
Ideally I would like to have VS pass values to 'someID' and 'anotherID' when debugging is started.
There are plenty of command-line tools that can execute a POST request without building a form; the simplest one is probably cURL.
curl -d someID=someValue&anotherID=someOtherValue http://localhost/myASPPage.asp
You could probably do a static HTML page that references jQuery and the following script that will execute a post on page load
$(document).ready(function(){
$.post("[url to your page goes here]", { someID: "someValue", anotherID: "someValue2" } );
});
Or, you could set your variables to use Request.QueryString() during testing and then reference your ASP page with variables via a URL
<%#LANGUAGE="JavaScript"%>
<%
var someID = new String( Request.QueryString("someID") );
var anotherID = new String( Request.QueryString("anotherID") );
%>
URL
http://myhost.com/myASPPage.asp?someID=1&anotherID=2
You could probably do this from your C# code using the WebClient.UploadValues method:
http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx
If you need to actually redirect the user to your classic ASP page with the values posted, you might have more difficulty (all of the standard methods of redirecting via ASP.NET only do GET requests, but my understanding is that you will need a POST request for the values to go into the Request.Form collection on the classic ASP page). A Server.Transfer call might be able to do what you want, and I also found some code here that claims to be able to do that:
http://www.codeproject.com/KB/aspnet/ASP_NETRedirectAndPost.aspx

Categories

Resources