简体   繁体   English

允许对 asp.net core 2.x 中的静态文件进行 POST 访问

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

I have mystuff.xml file in my wwwroot folder.我的 wwwroot 文件夹中有mystuff.xml文件。 Using postman I can issue a GET request to it just fine, since I added the following into Startup.cs使用邮递员我可以很好地向它发出 GET 请求,因为我将以下内容添加到 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.不幸的是,我正在使用第三方,它正在发出 POST 请求以获取该文件。 In Postman when I send a POST request to mystuff.xml, I get an empty response back.在 Postman 中,当我向 mystuff.xml 发送 POST 请求时,我得到一个空响应。 I'm assuming that only GET requests work for static files.我假设只有 GET 请求适用于静态文件。

Edit: I saw an overload for StaticFileOptions , but nothing in there seems like what I'm going for.编辑:我看到了StaticFileOptions的重载,但似乎没有什么是我想要的。

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.默认情况下,.NET 似乎不想为 POST 请求返回静态页面,而且我找不到启用它的相关选项。 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:在我的例子中,我只需要在根路径中的一个页面上 POST,所以这个代码块有效:

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.如果您需要处理通过 POST 请求的多个文件,则需要更强大的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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