简体   繁体   English

MVC WebApi高级自定义路由

[英]MVC WebApi advanced custom routing

I'm struggling with this for some time now. 我为此苦了一段时间。 I've searched all over the internet, didn't find any solution. 我在互联网上进行了搜索,没有找到任何解决方案。

I'd like to create a webapi project with somewhat custom routing. 我想用某种自定义路由创建一个webapi项目。 Using VS 2019, project is of type ASP.NET WebApi on .NET Core 2.2. 使用VS 2019,项目是.NET Core 2.2上的ASP.NET WebApi类型。 Routing should be like this: 路由应如下所示:

Basic application must reside on url similar to " https://my.server.com/myapi ". 基本应用程序必须驻留在类似于“ https://my.server.com/myapi ”的网址上 URLs which will be called are in form " https://my.server.com/myapi/ {InstanceName}/{CommandName}?{customParams}" 将被调用的URL的格式为“ https://my.server.com/myapi/ {InstanceName} / {CommandName}?{customParams}”

I have one controller defined in my project and I would like to redirect all requests to that controller, where instanceName could be parameter of all the methods contained in a controller, so I would get a value for that parameter. 我在项目中定义了一个控制器,我想将所有请求重定向到该控制器,其中instanceName可以是控制器中包含的所有方法的参数,因此我将获得该参数的值。 CommandName is basicly the same as "action" RouteData by MVC principles. 根据MVC原则,CommandName与“动作” RouteData基本相同。 As you can see there is no controller specified, since all is handled by one controller. 如您所见,没有指定控制器,因为所有控制器均由一个控制器处理。

So far I've tried setup routing like this: 到目前为止,我已经尝试过这样的设置路由:

Startup.cs 启动文件

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "MyRoute",
        template: "{instance}/{action}",
        defaults: new { controller = "MyController" });
    });
}

MyController.cs MyController.cs

[Route("/")]
[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("{instance}/info")]
    public JsonResult Info(string instance, InfoCommand model)
    {
        // Just return serialized model for now.
        var result = new JsonResult(model);

        return result;
    }
}

But this does not work. 但这是行不通的。 I get 415 response from (I think) web server when I call for example 例如,当我打电话时,我从Web服务器收到415响应

https://my.server.com/myapi/MYINSTANCE/info?param1=value1&param2=value2

While debugging from VS this URL looks like this: 从VS调试时,此URL如下所示:

https://localhost:12345/MYINSTANCE/info?param1=value1&param2=value2

but I think it shouldn't matter for routing. 但我认为路由无关紧要。

In best case scenario (putting [Route("{instance}")] above controller and [HttpGet("info")] above Info method) I get 404 response, which is also what I do not want. 在最好的情况下(将[Route("{instance}")]放在控制器上方,将[HttpGet("info")]放在Info方法上方),我得到404响应,这也是我所不希望的。

I've even tried creating my own ControllerFactory, but that didn't work either (changing controller inside ControllerFactory's create method and adding another parameter to RouteData). 我什至尝试创建自己的ControllerFactory,但这也不起作用(在ControllerFactory的create方法内更改控制器,并向RouteData添加另一个参数)。

How to setup routing like that? 如何设置这样的路由? Is it even possible? 可能吗 I would still like to use all other MVC features (model binding, proper routing, auth features, etc.), it's just this routing I cannot figure it out. 我仍然想使用所有其他MVC功能(模型绑定,正确的路由,身份验证功能等),只是这种路由我无法弄清楚。

Your attempt resulting a in 415 Unsupported Media Type error was your best one. 尝试导致415 Unsupported Media Type error是您最好的选择。
You were only missing the FromQuery as shown below. 您只缺少FromQuery ,如下所示。

The error indicates that the complex type InfoCommand could not be resolved. 该错误表明无法解析复杂类型的InfoCommand
You must specify that it must be parsed from the querystring. 您必须指定必须从查询字符串中进行解析。

Note that the route defined via MapRoute doesn't have effect, since you are using attribute-based routing; 请注意,由于您使用的是基于属性的路由,因此通过MapRoute定义的路由无效。 it's only one or the other. 只有一个或另一个。

[Route("/")]
[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("{instance}/info")]
    public JsonResult Info(string instance, [FromQuery] InfoCommand model)
    {
        var result = new JsonResult(model);
        return result;
    }
}

public class InfoCommand
{
    public InfoCommand()
    {}

    public string Param1 { get; set; }
    public string Param2 { get; set; }
}

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

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