Am working in asp.net and had to rewrite some urls rewriting is working fine here is an example I had to change URL mywebsite.com/search.aspx?cat=1 to mywebsite.com/search/cameras and it's working fine now I have to change page meta tags and when I try to get url by using
HttpContext.Current.Request.Url.PathAndQuery
am getting search.aspx?cat=1
while I want here is address written in address bar which is search/cameras
if it's not possible than is there any way to set meta tags for specific pages?
here is code for url rewrite
m_boolIsCustomPage = true;
m_strPageBaseUrl = "search.aspx";
if (m_intIDSearch > -1)
{
l_strQueryContents = m_intIDSearch.ToString();
m_intIDSearch = -1;
}
else
{
l_strQueryContents = "-1";
m_intIDSearch = -1;
}
HttpContext.Current.Request.RawUrl
As received by IIS prior to any manipulation.
Request.RawUrl vs. Request.Url
I have a ASP.NET MVC application which will work on a IIS7.
I don't know the final URL yet, and that's the problem.
I want to get the URL-Parts between the Top-level-domain and my controller.
For examople: http://www.mydomain.com/myApplication/MyController/ControllerMethodshould return /myApplication/MyController/
This should also be possible, if the application is called via the standard method, for example http://www.mydomain.com/myApplication.
The Problem is that with my method it works perfectly if the full controller- and methodname is in the url, but as soon as there is only the controller name and the route takes the default index-method or there is no controller/method and the route takes the default controller/method, it will fail because my code puts wrong output.
I thought about hardcoding the controller-name and make a if-then-else orgy, but this doesn't seem very professional...
Maybe anyone of you has got an Idea.
Here's my function:
String segments = Request.Url.Segments;
System.Text.StringBuilder builder = new System.Text.StringBuilder();
String lastSegment = "";
int i= 0;
do
{
builder.Append(segments[i]);
lastSegment = segments[i++];
} while(!lastSegment.Equals("Home") && !lastSegment.Equals("Home/") && i < segments.Length);
return builder.toString();
Use the Url class to build the Urls for you http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper(v=vs.118).aspx
I am using Mailgun API. There is a section that I need to provide a URL to them, then they are going to HTTP Post some data to me.
I provide this URL (http://test.com/MailGun/Webhook.aspx) to Mailgun, so they can Post data. I have a list of parameter names that they are sending like (recipient,domain, ip,...).
I am not sure how get that posted data in my page.
In Webhook.aspx page I tried some code as follows but all of them are empty.
lblrecipient.text= Request.Form["recipient"];
lblip.Text= Request.Params["ip"];
lbldomain.Text = Request.QueryString["domain"];
Not sure what to try to get the posted data?
This code will list out all the form variables that are being sent in a POST. This way you can see if you have the proper names of the post values.
string[] keys = Request.Form.AllKeys;
for (int i= 0; i < keys.Length; i++)
{
Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
}
This code reads the raw input stream from the HTTP request. Use this if the data isn't available in Request.Form or other model bindings or if you need access to the bytes/text as it comes.
using(var reader = new StreamReader(Request.InputStream))
content = reader.ReadToEnd();
You can simply use Request["recipient"] to "read the HTTP values sent by a client during a Web request"
To access data from the QueryString, Form, Cookies, or ServerVariables
collections, you can write Request["key"]
Source:
MSDN
Update: Summarizing conversation
In order to view the values that MailGun is posting to your site you will need to read them from the web request that MailGun is making, record them somewhere and then display them on your page.
You should have one endpoint where MailGun will send the POST values to and another page that you use to view the recorded values.
It appears that right now you have one page. So when you view this page, and you read the Request values, you are reading the values from YOUR request, not MailGun.
You are missing a step. You need to log / store the values on your server (mailgun is a client). Then you need to retrieve those values on your server (your pc with your web browser will be a client). These will be two totally different aspx files (or the same one with different parameters).
aspx page 1 (the one that mailgun has):
var val = Request.Form["recipient"];
var file = new File(filename);
file.write(val);
close(file);
aspx page 2:
var contents = "";
if (File.exists(filename))
var file = File.open(filename);
contents = file.readtoend();
file.close()
Request.write(contents);
Use this:
public void ShowAllPostBackData()
{
if (IsPostBack)
{
string[] keys = Request.Form.AllKeys;
Literal ctlAllPostbackData = new Literal();
ctlAllPostbackData.Text = "<div class='well well-lg' style='border:1px solid black;z-index:99999;position:absolute;'><h3>All postback data:</h3><br />";
for (int i = 0; i < keys.Length; i++)
{
ctlAllPostbackData.Text += "<b>" + keys[i] + "</b>: " + Request[keys[i]] + "<br />";
}
ctlAllPostbackData.Text += "</div>";
this.Controls.Add(ctlAllPostbackData);
}
}
In the web browser, open up developer console (F12 in Chrome and IE), then open network tab and watch the request and response data. Another option - use Fiddler (http://fiddler2.com/).
When you get to see the POST request as it is being sent to your page, look into query string and headers. You will see whether your data comes in query string or as form - or maybe it is not being sent to your page at all.
UPDATE: sorry, had to look at MailGun APIs first, they do not go through your browser, requests come directly from their server. You'll have to debug and examine all members of Request.Params when you get the POST from MailGun.
Try this
string[] keys = Request.Form.AllKeys;
var value = "";
for (int i= 0; i < keys.Length; i++)
{
// here you get the name eg test[0].quantity
// keys[i];
// to get the value you use
value = Request.Form[keys[i]];
}
In my case because I assigned the post data to the header, this is how I get it:
protected void Page_Load(object sender, EventArgs e){
...
postValue = Request.Headers["Key"];
This is how I attached the value and key to the POST:
var request = new NSMutableUrlRequest(url){
HttpMethod = "POST",
Headers = NSDictionary.FromObjectAndKey(FromObject(value), FromObject("key"))
};
webView.LoadRequest(request);
You can try to check the 'Request.Form.Keys'. If it will not works well, you can use 'request.inputStream' to get the soap string which will tell you all the request keys.
i writen this code
im using it for checking if a certin url is found on a web page
private void checkUrls (){
WebClient client;
for (int i = 0; i < Convert.ToInt32(txtnum.Text); i++) {
try
{
string Url = "http://www." + txtUrl.Text + i.ToString();
client = new WebClient();
string result = client.DownloadString(Url);
if (result.Contains(txtsearch.Text))
MessageBox.Show(Url);
}
catch (Exception ex) { }
the base url look like this:
http://www.example.com/?p=35
but on two sites when i ask for this:
http://www.example.com/?p=35
i get redirected to somthing like this
http://www.example.com/some_categoery/postitle/
i need to search the site in the first manner
but to download the content of the freindly url
can anyone show me the right direction to do so ?
i checking website where i do no know how many pages there on the site
You could try the HtmlAgilityPack to get all the anchor tags and check the href attribute for the value you want.
I am placing a JS file to remote server(s). I d like to know where the request is coming from.
ie : i have a js on google.com and upon user click on the link that s produced by js, it triggers some c# code on my server, but i also have the same js on yahoo.com and i d like to be able to know where the request is coming from.
How to find this ?
System.Environment.MachineName should contain the name of the server.
In ASP.NET, the referring page is given by Request.UrlReferrer as a Uri object.
This is also available as Request.ServerVariables["HTTP_REFERER"] as a string.
Request.ServerVariables["SERVER_NAME"]
Also you may need:
Request.ServerVariables["SERVER_PORT"]
Following on your own lead for "System.Web.HttpContext.Current.Request.ServerVariables", the MSDN documentation for the ServerVariables property contains some sample code on how to retrieve all available named server variables:
int loop1, loop2;
NameValueCollection coll;
// Load ServerVariable collection into NameValueCollection object.
coll=Request.ServerVariables;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++) {
Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}
Furthermore, it also contains a link to all server variables supported by IIS
IIS Server Variables
For example, the "REMOTE_HOST" variable will give you:
The name of the host that is making the request. If the server does not have this information, it will set REMOTE_ADDR and leave this empty.