简体   繁体   中英

allowing for POST access to static files in asp.net core 2.x

I have mystuff.xml file in my wwwroot folder. Using postman I can issue a GET request to it just fine, since I added the following into Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
     app.UseStaticFiles();  
}

Unfortunately I'm using a third party and it is making POST requests to get that file. In Postman when I send a POST request to mystuff.xml, I get an empty response back. I'm assuming that only GET requests work for static files.

Edit: I saw an overload for StaticFileOptions , but nothing in there seems like what I'm going for.

I also ran into this issue. .NET does not seem to want to return static pages for POST requests by default, and I couldn't find a relevant option to enable it. However, you can do it manually. In my case, I only needed POST for one page at the root path, so this code block worked:

app.UseEndpoints(endpoints =>
{
    endpoints.Map("/", async context =>
    {
        string page = await System.IO.File.ReadAllTextAsync("wwwroot/index.html");
        await context.Response.WriteAsync(page);
    });
});

A more robust solution would be needed if you need to handle multiple files requested via POST.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM