I am converting an old ASP.NET web forms site to ASP.NET MVC 5. I would like to issue permanent redirects for the old page URLs.
Here is what I have done -
RouteConfig.cs:
routes.MapRoute("About_old",
"About/About.aspx",
new { controller = "Home", action = "About_old" });
HomeController.cs:
public ActionResult About_old()
{
return new RedirectResult("/About", true);
// I've also tried
// return RedirectToActionPermanent("About");
// return RedirectPermanent("/About");
}
All attempts load the correct /About view, however the URL does not change, and I do not see a 301 code in the response. In other words, the URL is "localhost/About/About.aspx" and I expect it to be "localhost/About"
Complete Request/Repsonse from Chrome:
Request URL:http://localhost:55774/About/About.aspx
Request Method:GET
Status Code:200 OK
Request Headers
GET /About/About.aspx HTTP/1.1
Host: localhost:55774
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Response Headers
Cache-Control:private
Content-Encoding:gzip
Content-Length:2284
Content-Type:text/html; charset=utf-8
Date:Sat, 01 Mar 2014 18:10:41 GMT
Server:Microsoft-IIS/8.0
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.1
X-Powered-By:ASP.NET
Nowhere do I see a 301 and the URL does not change. I have to think this has to do with how I am mapping the route of the old aspx page in RouteConfig.cs as all action methods have the same results. NOTE I have put a solution using global.asax below, however I would prefer it to work as I am attempting above, so I have not accepted my answer.
Am I doing something wrong or just missing something? How do I get the 301 to issue and URL to change?
Here is my solution (Global.asax)
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
string currentUrl = HttpContext.Current.Request.Path.ToLower();
if (currentUrl.EndsWith("/about/about.aspx"))
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "/About");
Response.End();
}
}
From answer here: Global 301 redirection from domain to www.domain
I am trying to simulate a FileUpload with Chrome Postman to my ASP.NET WebApi method . To do this I perform the following steps in Postman.
1.Open postman and choose POST method from address bar.
2.Enter valid URL.
3.Go to Body section below address bar.
4.From Body section choose Binary option.
5.Hit the "Chose Files" button to select a file to upload.
6.Choose a file
7.Ensure the "Post" option is selected
8.Hit "Send"
I then test for MultiPartContent, however when I step through my method the Request Object doesn't appear to have the info that I need . The exception below will get hit.
public List<Premium.Model.BureauModUpdate> Post() {
HttpRequestMessage request = this.Request;
if (!request.Content.IsMimeMultipartContent()) {
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
}
It doesn't appear as if any info concerning the file upload is in the request. A review of the request content yields this:
{Method: POST, RequestUri: 'http://localhost/WCAPI/Lookup/BureauModUpdate/CreateNyModUpdates', Version: 1.1, Content: System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent, Headers:
{
Cache-Control: no-cache
Connection: keep-alive
Accept: */*
Accept-Encoding: gzip
Accept-Encoding: deflate
Accept-Encoding: br
Accept-Language: en-US
Accept-Language: en; q=0.8
Host: localhost
User-Agent: Mozilla/5.0
User-Agent: (Windows NT 6.1; WOW64)
User-Agent: AppleWebKit/537.36
User-Agent: (KHTML, like Gecko)
User-Agent: Chrome/55.0.2883.87
User-Agent: Safari/537.36
Postman-Token: 23f64855-3b60-3f87-3aa1-ba7d0a841273
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
Content-Length: 200284
Content-Type: application/x-www-form-urlencoded
}}
Is there something else I need to be doing ? If anybody has any suggestions or can point me in the right direction on this it would be greatly appreciated.
I believe your content type is incorrect. It should be multipart/form-data when uploading files.
application/x-www-form-urlencoded is used only when posting a form with no binary data.
I was wondering if I could get some help with this. A service is sending a POST request to my controller and I am trying to get the encoded URL so I can store the information in my database. Unfortnately I dont know exaclty what type of object it is I can use in order to refer to this encoded XML
This is the feed that is being sent by the service to my controller method:
POST https://localhost:44300/efile/GeneratingXMLFormResponse HTTP/1.1
Host: localhost:44300
Connection: keep-alive
Content-Length: 563
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: https://togatest.efiletexas.gov
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: https://togatest.efiletexas.gov/epayments/webs/EPaymentConfirmation.aspx
Accept-Encoding: gzip, deflate
Accept-Language: es,en-US;q=0.8,en;q=0.6
Cookie: _jsuid=1589536271; _okdetect=%7B%22token%22%3A%2214383525196380%22%2C%22proto%22%3A%22https%3A%22%2C%22host%22%3A%22localhost%3A44300%22%7D; _ok=9593-948-10-2895; .AspNet.ApplicationCookie=5vYkwGx8d4ALu7qNAHeDHrhm7VIfQXZ_xMqp8ns4Xt9UlklBWBcqjuRy_hv5hRtaTM8ZD08EXrvzusa4qooq_svaxPJC41PAx3mGPsLHR3cYbW0QpK0PNX5vNi0fNMlS7vKRcGDIpRjr2DSCBWY8XWBFihko2EBWxOdKrt-LK5-z6jauQrfrwWyGeHIsab9BHYr30iAfeOxrkvIVj9vguw7R6Gd9YPEpjgso3TdGc7tyQLv4FF0WB2dKzmv-5LkVIYp39QcGDyyXAJ79Z93JK1WYeF0flnI9gKYljyrvJaDfCFBBipanV2kY8h11RRqkPuI4KK3uXFjQbR4sh7zQHcCSLX37tAkwMLZUcVAm7V_KHPQhpEIH5F68tHivbE2zFk-uRq0_eASb9QMnxxIs8y2XL9ThRew4Z5DXToZrB4lUCFim0T1LGyOq7z5XvII-_N_jKic-M1X3O_pYN-heQWhwxlJN-QMEzdo7w4n_9t2PwL5Dzf9gtn01nqw2dV9oXAxhi_arkZ9vvjbe0WYYJg; _okac=5ad74dff9c0fd7f739a94f0c8e47f538; _okla=1; _okbk=cd5%3Davailable%2Ccd4%3Dtrue%2Cvi5%3D0%2Cvi4%3D1438875502265%2Cvi3%3Dactive%2Cvi2%3Dfalse%2Cvi1%3Dfalse%2Ccd8%3Dchat%2Ccd6%3D0%2Ccd3%3Dfalse%2Ccd2%3D0%2Ccd1%3D0%2C; heatmaps_g2g_100855693=no; _ga=GA1.1.845327345.1433349177; _gat=1; _oklv=1438876103374%2Ca8VFkRUOPj95wlmt5R2JW7AJFUNQE0Ib; calltrk_referrer=direct; calltrk_landing=https%3A//localhost%3A44300/Efile; calltrk_session_swap_numbers_215746334=5123841992%3D8775996483; clicky_olark=9593-948-10-2895,W23FqlSrwPRFNA555R2JW1Z3JUQ0EHN1,a8VFkRUOPj95wlmt5R2JW7AJFUNQE0Ib; _first_pageview=1; calltrk_session_id_215746334=acab75bc-fb76-4073-bc6a-1d4b5115975c; olfsk=olfsk03380024363286793; wcsid=a8VFkRUOPj95wlmt5R2JW7AJFUNQE0Ib; hblid=W23FqlSrwPRFNA555R2JW1Z3JUQ0EHN1
ResponseXML=%3CPaymentResponse%3E%3CClientKey%3ECJOFSTEXFILE%3C%2FClientKey%3E%3CTransactionID%3E44077405%3C%2FTransactionID%3E%3CAmount%3E-1.00%3C%2FAmount%3E%3CReferenceNumber%3E%3C%2FReferenceNumber%3E%3CPayorToken%3E54230445%3C%2FPayorToken%3E%3CPayorName%3EMarco+A+Lostaunau%3C%2FPayorName%3E%3CTenderDescription%3EDISCOVER+XXXXXXXXXXXX0000%3C%2FTenderDescription%3E%3CExpirationMonth%3E5%3C%2FExpirationMonth%3E%3CExpirationYear%3E2018%3C%2FExpirationYear%3E%3CPaymentTimestamp%3E8%2F6%2F2015+10%3A49%3A17+AM%3C%2FPaymentTimestamp%3E%3C%2FPaymentResponse%3E
As you can see the ResponseXML parameter is set to an encoded XML. I need to extract this information from this parameter decoded, deserialize it and take some of the nodes.
This is my controller method:
public ActionResult GeneratingXMLFormResponse(XmlDocument xmlResponse)
{
var form = xmlResponse;
return View("EfileView");
}
The xmlResponse does not have the information that I need.
What object can I use in order to refer to the parameter ResponseXML?
Hey guys I figured out how to do this.
If your parameter and XML are coming in the body of your resposen you can receive it in your controller method as a string. After receiving the string you can transform that string into an XML like so:
[HttpPost, ValidateInput(false)]
public string GeneratingXMLFormResponse(string responseXML)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(responseXML);
}
This is a follow-up question to the suggestion of user #Kiewic to make a post request using a webview: Post data with a request in a Windows Store app WebView - using C#
So I am using this code
// defined before: string uri, User user
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(uri));
request.Content = new HttpStringContent(
String.Format(
"language={0}&session_id={1}&user_id={2}",
Text.GetLanguage(),
user.session_id,
user.user_id.ToString()
),
Windows.Storage.Streams.UnicodeEncoding.Utf8,
"application/x-www-form-urlencoded"
);
webView.NavigateWithHttpRequestMessage(request); // webView defined in xaml
Fiddler shows me this request:
POST http://mobile.mysite.com/ HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US,en;q=0.8,de-CH;q=0.5,de;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; SMJB; WebView/2.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: mobile.mysyte.com
Content-Length: 101
Connection: Keep-Alive
Pragma: no-cache
Cookie: _ga=GA1.2.769009833.1387593479; PHPSESSID=nk6b04rb7d7vu4vmm2ish7l0o4
language=en&session_id=fhihferihguiegierewfrefghxrfer&user_id=1
The webserver technology is PHP, so I used this code to print the post content
<?php
var_dump($_POST);
?>
But the result is an empty array. So where is my mistake?
According to a Microsoft employee there is a bug with setting the headers of the request content. This should mean that nothing is wrong with my code in the first post:
http://social.msdn.microsoft.com/Forums/windowsapps/en-US/7deaf4ba-611f-4957-af3c-9b2b2e1e1b8b/webviewnavigatewithhttprequestmessage-set-contenttype?forum=winappswithcsharp
This means you cannot really use NavigateWithHttpRequest and you should use another approach to post data. User #Kiewic made a suggestion which works on Windows 8 too:
Post data with a request in a Windows Store app WebView - using C#
I'm trying to send to send the following header with my HttpWebRequest:
Connection: keep-alive
However, the header is never sent. Fiddler2 is showing that whenever I request the page in Google Chrome, the header is sent. However, my application refuses to send this header for some reason.
I have set the KeepAlive property to true (it's true by default anyway), yet the header still does not get sent.
I am trying to send this header with multiple HttpWebRequests, but they all basically look like this:
HttpWebRequest logIn6 = (HttpWebRequest)WebRequest.Create(new Uri(responseFromLogIn5));
logIn6.CookieContainer = cookies;
logIn6.KeepAlive = true;
logIn6.Referer = "https://login.yahoo.com/config/login?.src=spt&.intl=us&.lang=en-US&.done=http://football.fantasysports.yahoo.com/";
logIn6.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1";
logIn6.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
logIn6.Headers.Add("Accept-Encoding:gzip,deflate,sdch");
logIn6.Headers.Add("Accept-Language:en-US,en;q=0.8");
logIn6.Headers.Add("Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3");
logIn6.AllowAutoRedirect = false;
HttpWebResponse logIn6Response = (HttpWebResponse)logIn6.GetResponse();
string responseFromLogIn6 = logIn6Response.GetResponseHeader("Location");
cookies.Add(logIn6Response.Cookies);
logIn6Response.Close();
Does anyone know what I have to do to make sure this header is sent?
Fiddler2 Raw From Chrome:
GET xxx HTTP/1.1
Host: accounts.google.com
Connection: keep-alive
Referer: https://login.yahoo.com/config/login?.src=spt&.intl=us&.lang=en-US&.done=http://football.fantasysports.yahoo.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: xxx
HTTP/1.1 302 Moved Temporarily
Set-Cookie: xxx
Set-Cookie: xxx
Location: xxx
Content-Type: text/html; charset=UTF-8
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Date: Sat, 17 Sep 2011 22:27:09 GMT
Expires: Sat, 17 Sep 2011 22:27:09 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Content-Length: 2176
Server: GSE
Fiddler2 Raw From My Application:
GET xxx HTTP/1.1
Referer: https://login.yahoo.com/config/login?.src=spt&.intl=us&.lang=en-US&.done=http://football.fantasysports.yahoo.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Host: accounts.google.com
HTTP/1.1 302 Moved Temporarily
Location: xxx
Content-Type: text/html; charset=UTF-8
Date: Sun, 18 Sep 2011 00:05:40 GMT
Expires: Sun, 18 Sep 2011 00:05:40 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Content-Length: 573
Server: GSE
I'm trying to get the second Fiddler2 raw information to look like the first Fiddler2 raw information.
I've had the same issue: The Connection: Keep-Alive header is not sent except the first request, and the server I accessed won't give me the correct response if it is missing. So, here are my workarounds to this issue:
First is set the ProtocolVersion property of HttpWebRequest instance to HttpVersion.Version10. Except the http command will become GET xxx HTTP/1.0, it works and uses only the public API.
The second way uses the reflection to modify the internal property ServicePoint.HttpBehaviour of HttpWebRequest instance, like this:
var req = (HttpWebRequest)WebRequest.Create(someUrl);
var sp = req.ServicePoint;
var prop = sp.GetType().GetProperty("HttpBehaviour",
BindingFlags.Instance | BindingFlags.NonPublic);
prop.SetValue(sp, (byte)0, null);
req.GetResponse().Close();
Hope this helps.
I struggled with this problem for half a day! And dear old Fiddler (my guardian angel) was inadvertantly part of the problem:
Whenever I tested my HTTP POSTs using Fiddler monitoring ON - the problem DIDN'T appear
Whenever I tested my HTTP POSTs with Fiddler monitoring OFF - the problem DID appear
My POSTS were sent with protocol 1.1 and the Keep-Alive was ignored/redundant/why after the initial connection. i.e. I could see it in the header of the first POST (via Fiddler!), but not in subsequent POSTs despite using the same code. Hey ho ...
But the remote server would only respond if Keep-Alive was sent. Now I can't prove this, but I suspect that Fiddler monitoring the connection caused the remote server to think or believe that the connection was still active (despite no Keep-Alives sent after my first POST) and responded correctly. As I said, the second I turned Fiddler off, the absence of Keep-Alives caused the remote server to timeout on me..
I implemented the 1.0 solution described above and my POSTS now work, with or without Fiddler on or off. Hope this helps somebody else stuck somewhere ...
You doing it right. The code should result in following header added:
Connection: Keep-Alive
Post the code that you use for sending request and Raw output from Fiddler if you don't see this header. You may also ignore this because HTTP 1.1 connection is keep-alive by default.
Update: it looks like .NET only sets Keep-Alive explicitly for the first (!) request. Further requests to the same host/url will not have this header presumably because underlying tcp connection is already being reused.
After downloading HttpWebRequest source code, noticed that every property checks some known headers for HeaderCollection. To get rid of that doing some reflection stuff on that collection make it work
var webRequest = (HttpWebRequest) WebRequest.Create(url);
webRequest.Headers.GetType().InvokeMember("ChangeInternal",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
Type.DefaultBinder, webRequest.Headers, new object[] {name, value}
);
I know the answer for this as I had the same problem and managed to solve it by inheriting the webclient and overriding it's Get Web Request method.
See the code below:
public class CookieAwareWebClient : WebClient
{
public CookieContainer CookieContainer { get; set; }
public CookieAwareWebClient()
: this(new CookieContainer())
{ }
public CookieAwareWebClient(CookieContainer c)
{
this.CookieContainer = c;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var castRequest = request as HttpWebRequest;
if (castRequest != null)
{
castRequest.KeepAlive = true; //<-- this what you want! The rest you don't need.
castRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
castRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36";
castRequest.Referer = "https://www.jobserve.com/gb/en/Candidate/Login.aspx?url=48BB4C724EA6A1F2CADF4243A0D73C13225717A29AE8DAD6913D";
castRequest.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
castRequest.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6");
castRequest.CookieContainer = this.CookieContainer;
}
return request;
}
}
As you can see I am not only enabling keep-alive but I am utilizing cookies and other headers also!
I hope that helps!
Kiran