T4MVC and HTTPS not showing up in URL - asp.net-mvc-3

Im a bit confused but my URLs being rendered with T4MVC on my local IIS Express contain HTTPS in the URL. On our staging server the URL being rendered contains HTTP but are using the same code like below? Does anyone know why?
View
public static string GetSearchResultsUrl(this UrlHelper urlHelper, ActionResult actionResult)
{
return urlHelper.ActionAbsolute(actionResult.AddRouteValue(Config.ViewData.SearchResults, true));
}
public static string ActionAbsolute(this UrlHelper urlHelper, ActionResult result) {
return string.Format("{0}{1}",urlHelper.RequestContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority),
urlHelper.RouteUrl(result.GetRouteValueDictionary()));
UPDATE
I just found out a F5 load balancer is doing the redirect from http to https using a 302 redirect.
}

There were some recent changes to automatically use https, but that should only happen if you're using the [RequireHttps] attributes. Do you know if you're using that?
See the change log for details. This was added in 2.8.0/2.8.1.
Search for 'ActionUrlHttps' in t4mvc.tt to see the relevant code.

Related

Redirection from Spring Boot Application of External Website

I am building a Spring Boot Application with SSL enabled.
Now when I am redirecting to some external website with leading http:// explicitly.
for example http://www.example.com. But browser is redirecting it to https://www.example.com
automatically.
As I am not sure the target site is http or https. I want to redirect to the target as it is in my database.
Is there any one could help regarding this issue?
I have tried by returning redirect:http://www.example.com and ModelAndView approach. It did not work.
Note: My action can return html view or redirect to external site.
Try This :
#RequestMapping(value = "/redirect", method = RequestMethod.GET)
public void method(HttpServletResponse httpServletResponse) {
httpServletResponse.setHeader("Location", projectUrl);
httpServletResponse.setStatus(302);
}
I tried by different ways from the Application. But it never worked.
DevOps have added Load Balancer in the middle and something messed up while setting the Load Balancer rules. They have modified at their end. Now it's working fine.

How to forward to an external url?

My Spring based app is running under http://localhost. Another app is running under http://localhost:88. I need to achieve the following: when a user opens http://localhost/page, a content of http://localhost:88/content should be shown.
I've supposed, that I should use forwarding, like shown bellow:
#RequestMapping("/page")
public String handleUriPage() {
return "forward:http://localhost:88/content";
}
but seems like forwarding to an external URL doesn't work.
How can I achieve this behaviour with Spring?
Firstly, you specify that you want to show the content of "http://localhost:88/content" but you actually forward to "http://localhost:88" in your method.
Nevertheless, forward works with relative URLs only (served by other controllers of the same application), so you should use 'redirect:' instead.
Forward happens entirely on the server side: the Servlet container forwards the same request to the target URL, so the URL won't change in the address bar.
Redirect, on the other hand, will cause the server to respond with 302 and the Location header set to the new URL, after which the client browser will make a separate request to it, changing the URL in the address bar, of course.
UPDATE: For returning the content of the external page as it would be an internal one, I would write a separate controller method to make the request to the URL and just return its content. Something like the following:
#RequestMapping(value = "/external", produces = MediaType.TEXT_HTML_VALUE)
public void getExternalPage(#RequestParam("url") String url, HttpServletResponse response) throws IOException {
HttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet(url);
HttpResponse response1 = client.execute(request);
response.setContentType("text/html");
ByteStreams.copy(response1.getEntity().getContent(), response.getOutputStream());
}
Of course, you have many possible solutions. Here I used Apache Commons HttpClient for making the request, and Google's Guava for copying the response from that request to the resulting one.
After that, your return statement would change to the following:
return "forward:/external?url=http%3A%2F%2Flocalhost%3A88%2Fcontent"
Note how you need to encode your URL given as parameter.

WebApi Http Error 404 Not Found

I'm having a problem with WebApi 5.2.3 in which if I had a controller like so:
[RoutePrefix("api/values")]
public class ValuesController : ApiController
{
[Route]
public IHttpActionResult Get()
{
return Ok("data");
}
}
Currently if i make a request to api/values I retrieve my response "data" just fine. I would like to handle the case of routes that are not found, for example "api/values/foo". Currently making the api/values/foo request returns the typical IIS Http Error 404.0 Not Found page. I would like to be able to handle this and return a json or xml response based on the negotiated content type. Has anyone ran into this before and how did you solve it?
Thanks in advance.
Also to note, I created a DelegatingHandler and confirmed that my request is not entering the WebApi stack. Any ideas?
You might need to create your custom controller/action selector (IHttpControllerSelector/IHttpActionSelector), take a look into this article which creates a custom one to handle the 404 error.
http://weblogs.asp.net/imranbaloch/handling-http-404-error-in-asp-net-web-api
Hope this helps.

ASP.NET FileContentResult always 404 in production

I have an ASP.NET MVC 3 web app that's behaving strangely. There's an action that allows me to download a test file:
[HttpGet]
public ActionResult Download()
{
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "test_file.txt",
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(System.Text.Encoding.Unicode.GetBytes("HELLO THERE"), "text/plain");
}
In debug mode it works no problem, I get a file download as expected ("test_file.txt", with contents "HELLO THERE").
When published, and deployed with IIS 6, a 404 error is always returned. Anyone know why the difference and how to fix it?
Turns out the issue was a combination of routing and IIS configuration. I had some parameters for the action, one of which contained a file extension. I had set up a route so that the parameters were in the format
../Download/[text].txt
instead of
../Download?[text].txt
IIS was reading this as trying to access a file on the file system. I removed the custom route and all was resolved.

ASP.NET MVC2 and Browser Caching

I have a web application that fetches a lot of content via ajax. For example when a user edits some data, the browser will send the changes using an ajax post and then do an ajax get to get fresh content and replace an existing div on the page with that content. This was working just find with MVC1, but in MVC2 I would get inconsistent results.
I've found that MVC1 by default included an Expires item in the response headers set to the current time, but in MVC2 the Expires header is missing. This is a problem with some browsers (IE8) actually using the cached version of the ajax get instead of the fresh version.
To deal with the problem I created a simple ActionFilterAttribute that sets the reponse cache to NoCache (see below), which works, but it seems kind of sillly to decorate every controller with this attribute. Is there a global way to set this for every controller?
Is this a bug in MVC2 and it really should be setting the expires on every ActionResult/view/page? Don't most MVC programs deal with data entry where stale data is a very bad thing?
Thanks
Dan
public class ResponseNoCachingAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
filterContext.HttpContext.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
}
}
Have you tried adding the following attribute to your controller?
[OutputCache(Location = OutputCacheLocation.None)]

Resources