How to modify HttpContext.Request.Form in asp.net core

how to post data in asp net core
asp.net form submit example
request form getvalues
asp net post data
asp net core httpcontext in class library
httpcontext.current.request in asp.net core
mvc form submit
how to get html control value in mvc controller

I have an HttpContext.Request object that has data in the Form that is wrong and I want to fix it up and send the correct HttpContext on its way. HttpContext.Request.Form is readonly, but if it wasn't I would have simply done the following; HttpContext.Request.Form["a"] = "the correct value for a";

So, where is the best place in the pipeline to do this. Is it possible to make the HttpContext.Request.Form write accessable via reflection?

This was easier than I thought. I am doing this in my middleware which is there to correct bad form data that came in.

public async Task Invoke(HttpContext context)
{
    ....
    NameValueCollection fcnvc = context.Request.Form.AsNameValueCollection();
    fcnvc.Set("a", "the correct value of a");
    fcnvc.Set("b", "a value the client forgot to post");
    Dictionary<string, StringValues> dictValues = new Dictionary<string, StringValues>();
    foreach (var key in fcnvc.AllKeys)
    {
      dictValues.Add(key, fcnvc.Get(key));
    }
    var fc = new FormCollection(dictValues);
    context.Request.Form = fc;
    ....
    await _next.Invoke(context);
}

Interestingly the FormCollection is readonly, but the HttpContext.Request object is not thus allowing me to replace the entire Form.

Change Request.Form collection on the fly, Request.Form property is ReadOnly. Than means that Set method is not declared on this property. Web.HttpContext.Current.Request.Form; var propInfo = collection. NET programming platform including ASP. ASP.NET Core using SmtpClient and dependency injection; 864Differences in Time Zone  ASP.NET Core apps access HttpContext through the IHttpContextAccessor interface and its default implementation HttpContextAccessor. It's only necessary to use IHttpContextAccessor when you need access to the HttpContext inside a service. Razor views expose the HttpContext directly via a RazorPage.Context property on the view.

AsNameValueCollection is inside of IdentityServer4.dll.

public static class IReadableStringCollectionExtensions
{
    [DebuggerStepThrough]
    public static NameValueCollection AsNameValueCollection(this IDictionary<string, StringValues> collection)
    {
        NameValueCollection values = new NameValueCollection();
        foreach (KeyValuePair<string, StringValues> pair in collection)
        {
            string introduced3 = pair.get_Key();
            values.Add(introduced3, Enumerable.First<string>(pair.get_Value()));
        }
        return values;
    }

    [DebuggerStepThrough]
    public static NameValueCollection AsNameValueCollection(this IEnumerable<KeyValuePair<string, StringValues>> collection)
    {
        NameValueCollection values = new NameValueCollection();
        foreach (KeyValuePair<string, StringValues> pair in collection)
        {
            string introduced3 = pair.get_Key();
            values.Add(introduced3, Enumerable.First<string>(pair.get_Value()));
        }
        return values;
    }
}

Forms (POST data), A POST request is usually initiated when the user submits a FORM (like a the HttpContext class to pick up POST data: The Form property. In ASP.NET MVC, the Notice how I set the method attribute to POST - forms support the GET method  And I need to modify it for all my requests. (This obj from external resource so I can't add additional property, and I have really lot of methods using this obj so I can't modify property all time where I need it, also if I inherit this object I will need to do a lot of refactoring which are not allowing on the current project cycle.)

A bit complex but shorter solution

var collection = HttpContext.Request.Form;
var propInfo = collection.GetType().GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
propInfo.SetValue(collection, false, new object[]{});
collection.Remove("a");
collection.Add("a", "the correct value for a");

System.Diagnostics.Debug.Write(HttpContext.Request["a"]); // the correct value for a

Enjoy!

Form, Query String and HttpContext in ASP.NET Core 1.0 , ASP.NET Core is a modern web development framework. Features Request.​Form["firstname"]; string lastName = HttpContext.Request. Now, change the method of the <form> element to GET and change the asp-action to  Uploading file in Asp.Net Core 2.0 is done with an interface IFormFile that you would take as a parameter in your post action. Lets say you have an ajax post that will call the POST action and upload a file. You would first create the form data.

Access HttpContext in ASP.NET Core, aspx page without using the fully qualified class reference to HttpContext. For example, you can use Request.Browser to get the capabilities of the client browser. Hi, I am develop a sample asp.net mvc 5 app. And encounter a strange issue within mvc controller action. Scenario description, In mvc view, I've include a jQuery file upload plugin, the plupload.

HttpContext.Request Property (System.Web), I have an HttpContext.Request object that has data in the Form that is wrong and I want to fix it up and send the correct HttpContext on its way. HttpContext. Wire up your inputs (text boxes etc) to something on the model (using Tag Helpers) and the value entered by the user will be submitted as formdata when the user submits the form. ASP.NET Core’s model binding will then kick in and assign the posted values to an instance of the Model. From here, you can do whatever you need to with the model

How to modify HttpContext.Request.Form in asp.net core - php, HttpContext is largely the same in asp.net core as it's always been. public async Task Invoke(HttpContext httpContext) { var apiKey = httpContext.Request. In summary, after setting this up correctly in Startup, you can make  ASP.NET Core is a modern web development framework. Features such as model binding make your life easy by mapping form field values with model properties. So, accessing form and query string values manually might not be needed so often. However, at times you may need to read the form collection and query string values manually. The same can be said about Items collection of HttpContent. This

Comments
  • What exactly do you mean by "send the correct HttpContext on its way?" Are you passing it to another method/object or are you trying to send it back to the client?
  • I am interceping incoming requests and fixing up bad form data in middleware, and sending good form data down the rest of the incomming pipeline.
  • I don't have AsNameValueCollection extension method! where is it?
  • AsNameValueCollection is inside of IdentityServer4.dll, I posted the source code below.