Where to use GetBufferlessInputStream? - c#

Where do I set HttpContext.Request.GetBufferlessInputStream(true)? I am trying to allow the user to upload files larger than 2GB and obviously I am running into the "maxRequestLength" int type restriction. I have tried to create a StreamReader the following way:
var reader = new StreamReader(HttpContext.Request.GetBufferlessInputStream(true));
But I'm doing it in a controller and I end up getting the following error:
"This method or property is not supported after HttpRequest.Form, Files, InputStream, or BinaryRead has been invoked."
So I'm guessing I have to make this change before the controller method gets called. I've searched stack overflow and many other websites for answers, but all I've found is how to use it not where to use it.
Thank you for your time and helping me out with this.

You are going to have to implement your logic in an HttpModule -
see this similar question: How should I use HttpRequest.GetBufferlessInputStream?
Update - actually you are better off writing your own HttpHandler instead of a Module
Using GetBufferlessInputStream is basically telling ASP.NET that you are going to handle the entire request, instead of only a portion of it. As you've seen when you use this in a module, after your module completes, the request continues through the remainder of the request pipeline, and ASP.NET is going to expect to be able to read part of the input.
By writing your own HttpHandler, you are responsible for the entirety of the processing - so this would only be for handling the large upload.
I found a link with a sample that looks pretty relevant for you https://blogs.visoftinc.com/2013/03/26/streaming-large-files-asynchronously-using-net-4-5/

Related

Multiple Serilog Loggers

Is it possible to have multiple serilog loggers? Currently within my WebApi I can call Log.Information for example to log an information event, but is there a way that I can instead make different logs and call ExternalLog.Information or AuthenticationLog.Information from my controller? The purpose behind this is that my web api is currently working with multiple different databases for different yet interrelated projects, and I would like to store logs within each of these databases that pertain to them instead of needing to create an additional logging database if at all possible.
A better solution, that I figure is less likely is, can I map individual controllers to a log, so that any time that a specific controller calls log, it writes to the AuthenticationLog for example.
I believe that the answer to this question is to use subloggers, rather than separate loggers. I have found that you can do .WriteTo.Logger and filter further in there. I will accept this as the answer if nobody else has a better solution (and of course if I am able to get it to work). I need to be able to filter on the controller or action name, which at this time I have a second stack overflow question out to figure out how to get that data. Serilog with Asp.net Web Api not using enricher

Add rel="nofollow" automatically to all outbound links in ASP.NET

Any geniuses on StackOverflow ever made a solution which automatically adds rel="nofollow", to all external links?
I'd just like to apologise, I'm very new to backend coding - my attempts have literally got nowhere hence why I haven't posted them.
I've found some solutions in php, but nothing in ASP.NET.
I have a solution in jQuery, but the issue is - it'll be added after load. This is no good for telling Googlebot to ignore said links on my pages.
The jQuery solution is:
$("div.content a[href^='http']:not([href*='mysite.co.uk'])").attr("rel",
"follow");
One way would be to create your own custom HttpModule that set's the response to use a derived stream class to filter the HTTP body. There is a linked example in there on how to create a basic HttpModule. Github or Nuget may have a filter class that someone has written to do modifications the output stream when it's content type is text/html that you might be able to modify for your needs.
To build one on your own you will essentially need to attach to the BeginRequest event and set a filter to the HttpApplication's response.filter. That filter will be in charge of implementation of reading the response the page/control/ihttphandler has created and modifying it before it sends it to the client and then implementing the write to the client.

Is there a way to list all Elastic Load Balancers? (Using AWS .NET SDK)

I am trying to get the status of my Load Balancers programmatically and that proves quite problematically as I have to enter an incredibly long string for the ARN (Or I guess with the Name, which is shorter, I get the same result, but that's beside the point). If I keep setting up new ELBs and deleting the old ones (just assume I would do such a weird thing) I'm having a hard time keeping track of all of my ELBs that are currently set up.
Ideally, I would want to use a command that outputs a list of all of my ELB's Names or ARNs and with that, I can call the API as in the sample below.
AmazonElasticLoadBalancingV2Client balancingClient = newAmazonElasticLoadBalancingV2Client(region: regEndpoint);
var response = balancingClient.DescribeLoadBalancers(new DescribeLoadBalancersRequest
{
LoadBalancerArns =
{
//Incredibly long string
//Might be amazing to replace this
//with a fancy little Method that just
//returns a string or an array of strings
}
});
var loads = response.LoadBalancers;
Do you by any chance know a way to get that?
If you just call DescribeLoadBalancers() without passing it a list of load balancers to describe, it will return a list of all your load balancers.
I ran into problems like this, and built a solution I call the AWS Trycorder. I have been adding more and more data to the AWS Trycorder, which basically is just an information gatherer of AWS account data across all our accounts. It is hosted on Github, and one of the libraries contains code for sucking up data from various AWS services. You are welcome to mine it for functions, or use it directly. It gets tricky, as sometimes the data cannot be collected from a single request, but a specific request has to be made for each resource. For example, if you want to find out where the log is for a beanstalk instance, you have to request data specifically for that instance. The library tries to make all those calls so the data you need is in a single table.
There is a website at http:\trycorder.stiv.com that gives an overview, but the site does not list a lot of the new features.

Is Request.QueryString without ? or & possible?

Is it possible to get the QueryString value without using ? or & in the url?
I would like to have it like this:
http://www.colors.com/Red
string id = Request.QueryString["?"];
Instead of following:
http://www.colors.com/?ColorID=Red
string id = Request.QueryString["ColorID"];
No. a query string is defined by the appearance of a ?.
The example you give would redirect the user to a directory.
If you want to still be able to access the value of color-id through Querystring, then you should look at Rewriting. This can be due to legacy code that you can't change or other forms of interacting with 3rd party code. The benefit or Rewriting is that the code that ends up being executed doesn't know how the url looked like before it was rewritten and it can keep working as if there were a Querystring parameter named ColorID.
In its simplest form you need to call the Rewrite method of HttpContext, which will spin up a new request internally that executes code that matches that url without the user noticing anything. One caveat of this can be, that your legacy code doesn't know how to render correct links in menus and stuff, so you would keep having urls like ?ColorID=Red where it should have been just Red.
In IIS 7 and up, there is a built in filter where you can write your rules and patterns so you don't need to write your own code that matches incoming requests and calls HttpContext.Rewrite. Read more about it here on MSDN.
Now, Routing is a whole other thing. Its a Asp.net feature and doesn't work on top of existing legacy code but needs to be used with it. Meaning that the executing code needs to know that the request was routed to it. This of course has many benefits and of you're writing a new system then i would definitively recommend using Routing over Rewriting. There is a good article here about the differences and some SO questions also cover the topic:
IIS URL Rewriting vs URL Routing
Url Rewriting vs. Routing
It sounds like you may want to implement an MVC website.
Take a look at this MSDN documentation for more information.

Responding to HTTP POST

I'm not sure if I'm asking the right question.
We have a web app that we're trying to have a 3rd party POST to. We're creating a special landing page for them to which they can submit the data we need via POST.
I'm not sure how to respond to their request, which I assume I handle as an incoming HttpRequest. Do I process their data in PageLoad or some other event? Where/How is this data contained?
Do I have to use HttpListener or the ProcessRequest handler, or what?
Doing a search here or on Google turns up a lot of results on how to POST to another site, but can't seem to find a relevant site on how to be that "other" site and handle the incoming POST data.
Again, I'm not sure I'm asking this right.
EDIT: I found the Page.ProcessRequest Method in the MSDN library, but the Remarks say "You should not call this method"
Thanks!
You really need to look at the basics of ASP.NET. Even if this were a case where an IHttpHandler would be best-suited, I'd suggest using an .aspx page in this case as it's the best place to begin learning, and you can move to an IHttpHandler later on.
If the data is posted in application/x-www-form-urlencoded or multipart/form-data (the two formats used by forms on web pages - if they haven't told you what format they are using then it's probably one of those two), the Request.Form property (actually, a property of a property) will act as a dictionary into the data sent (e.g. if they have a field called "foo" then Request.Form["foo"] wll return the value of it as a string). Otherwise you'll want to use the Request.InputStream and read from that. This latter is a tiny bit more involved though.
Best would be to use an IHttpHandler, but it is possible to do what you want using a standard ASP.NET Page. Using PageLoad is fine, you have access to the Request and Response properties, which give you everything you need to process an HTTP request. For example, to obtain form parameters, you can use Request["input1"] to get the form input value (either query string, form post, or cookie) with the name "input1".
What is it you need to do in response to this post request? What sort of data do you need to return? Until that is answered, hard to help further.

Categories

Resources