net8 webapi

直接用net8 web api模板,创建时不要选控制器,直接添加类文件,并调整基类等即可

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System.Text.Unicode;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class HelloController : ControllerBase
    {
        [HttpGet]
        public string Get1()
        {
            JObject ob = new JObject();
            ob["姓名"] = "ljq";
            ob[""] = 40;

            string str = ob.ToString();
            return str;
        }

        [HttpGet]
        public string Get2(string str, int m)
        {
            JObject ob = new JObject();
            ob["姓名"] = str;
            ob[""] = m;

            string t = ob.ToString();
            return t;
        }
        [HttpGet("{str}/{m}")]
        public string Get3(string str, int m)
        {
            JObject ob = new JObject();
            ob["姓名"] = str;
            ob[""] = m;

            string t = ob.ToString();
            return t;
        }

        [HttpGet("{str}/{m}")]
        [HttpPost("{str}/{m}")]
        public async Task<string> p1(string str, int m)
        {
            JObject ob = new JObject();
            foreach (var item in this.Request.Headers)
            {
                ob[item.Key] = item.Value.ToString();
            }
            StreamReader r = new StreamReader(this.Request.Body);
            string b = await r.ReadToEndAsync();

            ob["b"] = b;
            string t = ob.ToString();
            return t;
        }


        [HttpPost]
        [RequestSizeLimit(int.MaxValue)]
        [RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue)]
        public async Task<string> p2()
        {
            //这个可以接收字符串,也可接收二进制,可以替代p1函数
            JObject ob = new JObject();
            foreach (var item in this.Request.Headers)
            {
                ob[item.Key] = item.Value.ToString();
            }

            MemoryStream ms = new MemoryStream(2);
            await this.Request.Body.CopyToAsync(ms);
            byte[] bytes = ms.ToArray();

            string str = System.Text.Encoding.UTF8.GetString(bytes);
            ob["b"] = str;
            string t = ob.ToString();
            return t;
        }

        [HttpPost]
        //[RequestSizeLimit(int.MaxValue)]
        //[RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue)]
        [DisableRequestSizeLimit]
        public async Task<string> p3()
        {
            //这个可以接收字符串,也可接收二进制,可以替代p1函数
//默认上传30M以下,如果超过则加上面的配置
JObject ob = new JObject(); foreach (var item in this.Request.Headers) { ob[item.Key] = item.Value.ToString(); } MemoryStream ms = new MemoryStream(2); await this.Request.Body.CopyToAsync(ms); byte[] bytes = ms.ToArray(); FileStream fs = new FileStream("d:\\1.zip", FileMode.Create); fs.Write(bytes, 0, bytes.Length); ms.Close(); fs.Close(); return "ok"; } [HttpPost] public async Task<string> p4() { //这个可以接收字符串,也可接收二进制,可以替代p1函数 JObject ob = new JObject(); foreach (var item in this.Request.Headers) { ob[item.Key] = item.Value.ToString(); } MemoryStream ms = new MemoryStream(2); await this.Request.Body.CopyToAsync(ms); byte[] bytes = ms.ToArray(); FileStream fs = new FileStream("d:\\1.zip", FileMode.Create); fs.Write(bytes, 0, bytes.Length); ms.Close(); fs.Close(); return "ok"; } } }

 

在iis上发布要安装:dotnet-hosting-8.0.1-win.exe

直接执行exe文件可以发布,在Windows2019 IIS发布成功,在Windows10 iis发布失败

上传大于30M的问题,在IIS上发布时,需要在Web.config中修改:

<system.webServer>中加入:
<security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483647">        </requestLimits>
      </requestFiltering>
</security>

以上测试调用未加  [DisableRequestSizeLimit]的p3接口上调用成功,但在p4接口上反而失败。

如果直接用exe发布web则不需web.config中配置最大值,直接使用   [DisableRequestSizeLimit]

 

posted @ 2024-02-07 10:55  81  阅读(144)  评论(0编辑  收藏  举报