网页API了解路由 [英] Web APi Routing Understanding

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

问题描述

我创建了一个名为WebPortalController新的控制器,但是当我把它或尝试通过我想不出访问下面的方法,只是说资源没有找到浏览器调用它。我需要一个新的路由添加到RoutesConfig.cs code如果又如何?

 命名空间WebApi.Controllers
{
公共类WebPortalController:ApiController
{
    //获取API /门户网站
    私人WebPortEnterpriseManagementDa _da =新WebPortEnterpriseManagementDa();
    公共ManagedType获取(字符串名称)
    {        ManagedType项目= _da.GetManagedType(名);        返回的物品;
    }
    // POST API /门户网站
    公共无效后([FromBody]字符串值)
    {
    }    // PUT API /门户网站/ 5
    公共无效认沽(INT ID,[FromBody]字符串值)
    {
    }    //删除API /门户网站/ 5
    公共无效删除(INT ID)
    {
    }
}
}

路线文件

 命名空间的WebAPI
{
公共类RouteConfig
{
    公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapRoute(
            名称:默认,
            网址:{控制器} / {行动} / {ID}
            默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
        );
    }
}


解决方案

您已经显示路由的配置是MVC路线和位于App_Start / RouteConfig文件。检查你有你的App_Start / WebApiConfig设置默认路由的API:

  config.Routes.MapHttpRoute(
    名称:DefaultApi
    routeTemplate:API / {}控制器/(编号),
    默认:新{ID = RouteParameter.Optional}
);

然后你需要在你的Get方法来改变参数名称相匹配的路由参数:

 公共ManagedType获取(字符串ID)
{
    ManagedType项目= _da.GetManagedType(名);
    返回的物品;
}

这应该允许您使用通过浏览器调用你的Get方法:

 本地主机:55304 / API /门户网站/测试

为了测试你的帖子/认沽/删除方法,你将需要使用提琴手或浏览器插件,如邮差

I have created a new controller called WebPortalController but when I call it or try to call it via the browser I couldnt access the below method just said resource is not found. Do I need to add a new routing to the RoutesConfig.cs code if so how.?

namespace WebApi.Controllers
{
public class WebPortalController : ApiController
{
    // GET api/webportal
    private WebPortEnterpriseManagementDa _da = new WebPortEnterpriseManagementDa();


    public ManagedType Get(string name)
    {

        ManagedType items = _da.GetManagedType(name);

        return items;
    }


    // POST api/webportal
    public void Post([FromBody]string value)
    {
    }

    // PUT api/webportal/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/webportal/5
    public void Delete(int id)
    {
    }
}
}

Routes file

namespace WebApi
{
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );


    }
}

解决方案

The route config you have shown is for MVC routes and is located in the App_Start/RouteConfig file. Check that you have a default API route set in your App_Start/WebApiConfig:

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

Then you will need to change the parameter name in your Get method to match the routing parameter:

public ManagedType Get(string id)
{
    ManagedType items = _da.GetManagedType(name);
    return items;
}

This should allow you to call your Get method through a browser using:

localhost:55304/api/WebPortal/Test

In order to test out your Post/Put/Delete methods you will need to use Fiddler or a browser addin such as Postman

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

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