接受.NET核心中URL的参数

接受.NET核心中URL的参数,.net,asp.net-core-mvc,.net,Asp.net Core Mvc,我是.NETCore的新手,我想知道是否有可能更改请求URL的默认格式。让我解释一下。我在控制器上有两个get方法,用于获取列表和1个元素的信息 [Route("api/[controller]/")] [ApiController] public class Department : ControllerBase { [HttpGet] public IEnumerable<string> Get() { return n

我是.NETCore的新手,我想知道是否有可能更改请求URL的默认格式。让我解释一下。我在控制器上有两个get方法,用于获取列表和1个元素的信息

[Route("api/[controller]/")]
[ApiController]
public class Department : ControllerBase
{
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    [HttpGet("{id:int}")]
    public string GetDepartamento(int id)
    {
        return "value";
    }

    [HttpPost]
    public void Post([FromBody] string value)
    {
    }
  }
}
但我需要使用这种格式,不仅仅是这个控制器,而是所有的控制器

GET:https://localhost:44309/api/department
GET:https://localhost:44309/api/department?id=1
POST:https://localhost:44309/api/department (data on the body of http call)
PUT:https://localhost:44309/api/department?id=1 (data on the body of http call)
DELETE:https://localhost:44309/api/department?id=1
我正在处理一个新项目(.net核心web应用程序MVC),所以启动和程序文件没有被修改

我在控制器上有两个get方法,用于获取列表和1个元素的信息

[Route("api/[controller]/")]
[ApiController]
public class Department : ControllerBase
{
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    [HttpGet("{id:int}")]
    public string GetDepartamento(int id)
    {
        return "value";
    }

    [HttpPost]
    public void Post([FromBody] string value)
    {
    }
  }
}
我需要使用这种格式

GET:https://localhost:44309/api/department

GET:https://localhost:44309/api/department?id=1

如果希望根据查询字符串将请求与预期操作相匹配,可以尝试实现自定义
操作方法SelectorAttribute
,并将其应用于操作,如下所示

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class CheckQueryStingAttribute : ActionMethodSelectorAttribute
{
    public string QueryStingName { get; set; }
    public bool CanPass { get; set; }
    public CheckQueryStingAttribute(string qname, bool canpass)
    {
        QueryStingName = qname;
        CanPass = canpass;
    }

    public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action)
    {
        StringValues value;

        routeContext.HttpContext.Request.Query.TryGetValue(QueryStingName, out value);

        if (QueryStingName == "" && CanPass)
        {
            return true;
        }
        else
        {
            if (CanPass)
            {
                return !StringValues.IsNullOrEmpty(value);
            }

            return StringValues.IsNullOrEmpty(value);
        }
    }
}
将其应用于行动

[HttpGet]
[检查查询字符串(“id”,false)]
[检查查询字符串(“,true)]
公共IEnumerable Get()
{
返回新字符串[]{“value1”,“value2”};
}
[HttpGet]
[CheckQueryString(“id”,true)]
[检查查询字符串(“,false)]
公共字符串getDepartmento(int-id)
{
返回“值”;
}
测试结果


Hi@Kevtho,有关于这个案子的最新消息吗?
[HttpGet]
[CheckQuerySting("id", false)]
[CheckQuerySting("", true)]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}

[HttpGet]
[CheckQuerySting("id", true)]
[CheckQuerySting("", false)]
public string GetDepartamento(int id)
{
    return "value";
}