Webapi路由问题 [英] Webapi routing issue

查看:58
本文介绍了Webapi路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是webAPI的新手。我有一个现有的webAPI项目,我需要运行/ debug.This项目使用基于属性的路由为每个操作方法。在RouteConfig.cs文件中,他们有{controller} / {action}作为MapRoute方法中的URL参数(请注意他们从那里删除了api /部分。但是我仍然无法像这样调用web服务 - >http:// localhost / {controller} / {action}。但是,我能够在控制器名称之前加上前缀api /之后调用它们,即http:// localhost / api / {controller} / {action}。



有人可以帮助我找出可以定义这个路由配置的其他地方?



谢谢



我是什么尝试过:



更改路由配置

Hi,

I am a newbie to webAPI. I have an existing webAPI project which i need to run/debug.This project used Attribute based routing for each action method.In RouteConfig.cs file they have {controller}/{action} as URL parameter in MapRoute method(Please note that they have removed the "api/" part from there).But still I'm not able to call the webservices like this ->"http://localhost/{controller}/{action}".However, i am able to call them after prefixing "api/" before controller name i.e. "http://localhost/api/{controller}/{action}".

Can someone help me to figure out where else this routing configuration may be defined?

Thanks

What I have tried:

Changing routing configurations

推荐答案

Web API配置不在您的RouteConfig.cs,而不是您的WebApiConfig.cs文件。在这里你会找到api /的前缀。



Web API configuration isn't in your RouteConfig.cs, rather your WebApiConfig.cs file. Here you will find the prefix of api/.

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }





您需要某种前缀(api /),因为它的属性会有混淆路由或普通的MVC类型路由。



但我认为你的问题的主要来源是你需要查看WebApiConfig.cs文件。



You'll need the prefix (api/) of some sort as there will be confusion whether its an attribute route or a normal MVC type route.

But i think the primary source of your issue is you need to be looking in the WebApiConfig.cs file.


这篇关于Webapi路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆