简体   繁体   English

WebApi 硬编码控制器路由

[英]WebApi hard coded controller routing

I am trying to write a self hosted WebAPI server.我正在尝试编写一个自托管的 WebAPI 服务器。 I want all routes to go to a single controller.我希望所有路由都转到一个控制器。 This controller can pick out the controller part of the url and use this to decide an appropriate response.该控制器可以选择 url 的控制器部分并使用它来决定适当的响应。

I have the following route configuration:我有以下路由配置:

_configuration.Routes.MapHttpRoute
(
    name: "DefaultApi",
    routeTemplate: string.Concat("api/Home", "/{id}"),
    defaults: new { id = RouteParameter.Optional, controllerName="Home" }
);

My controller class is called "HomeController".我的控制器类称为“HomeController”。 I'm trying to redirect all URLs to it.我正在尝试将所有 URL 重定向到它。

Here is the code for HomeController.这是 HomeController 的代码。 For now I have commented out the calls to external logic (remote controller).现在我已经注释掉了对外部逻辑(远程控制器)的调用。 It should just be returning a string on the Get action.它应该只是在 Get 操作上返回一个字符串。

public class HomeController : ApiController
{
    private IExternalController<string> remoteController;

    public HomeController()
    {
        remoteController = GlobalKernelConfiguration.GetStandardKernel().Get<IExternalController<string>>();
    }

    public string Get()
    {
        return "HELLO FROM INTERNAL"; //remoteController.Get();
    }

    public string Get(int id)
    {
        return remoteController.Get(id);
    }

    public void Delete(int id)
    {
         remoteController.Delete(id);
    }

    public void Post(string value)
    {
        remoteController.Post(value);
    }

    public void Put(int id, string value)
    {
        remoteController.Put(id, value);
    }
}

I would expect http://localhost:9000/api/[AnythingHere] to route to the home controller but I get the following error when trying the following url: http://localhost:9000/api/Home我希望http://localhost:9000/api/[AnythingHere]路由到家庭控制器,但在尝试以下 url 时出现以下错误: http://localhost:9000/api/Home

{"Message":"No HTTP resource was found that matches the request URI ' http://loca lhost:9000/api/Home'.","MessageDetail":"No route providing a controller name was found to match request URI ' http://localhost:9000/api/Home '"} {"Message":"未找到与请求 URI ' http://loca lhost:9000/api/Home'匹配的 HTTP 资源。","MessageDetail":"未找到提供控制器名称的路由以匹配请求 URI ' http://localhost:9000/api/Home '"}

As @CodeCaster suggested in the comments the problem was caused by not using the correct parameter in the routing options.正如@CodeCaster 在评论中所建议的,问题是由于没有在路由选项中使用正确的参数引起的。

This is what I had before这是我以前的

_configuration.Routes.MapHttpRoute ( name: "DefaultApi", routeTemplate: string.Concat("api/Home", "/{id}"), defaults: new { id = RouteParameter.Optional, controllerName="Home" } );

this is what I have now:这就是我现在所拥有的:

        public static void AddControllerRoute(string controllerName)
    {
        _configuration.Routes.MapHttpRoute
           (
               name: "DefaultApi",
               routeTemplate: string.Concat("api/Home", "/{id}"),
               defaults: new { id = RouteParameter.Optional, controller ="Home" }
           );
    }

notice that the defaults parameter was changed and now uses "controller" instead of "controllerName" this solved the problem and it's now working.请注意,默认参数已更改,现在使用“controller”而不是“controllerName”,这解决了问题,现在可以正常工作了。

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

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