ASP.Net Web API - 查找正确的URL

时间:2014-11-12 14:08:30

标签: asp.net asp.net-mvc razor asp.net-web-api entity

这可能是一个简单的问题。我已经为Web API实现了一个脚手架控制器,但似乎无法在浏览器中找到正确的URL。

控制器名为ComputerAddController.cs

我试过了:

http://localhost:port/api/ComputerAdd/2
http://localhost:port/api/ComputerAdd
http://localhost:port/ComputerAdd/api/2
http://localhost:port/ComputerAdd/api

任何方向都会非常感激(DB / Model肯定在id 2处有一个现有的条目)

我的Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Http;
using System.Web.Routing;

namespace Inbound
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }
}

WebApiConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace Inbound
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

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

控制器:

public class ComputerAddController : ApiController
    {
        private InboundModel db = new InboundModel();

        // GET: api/ComputerAdd
        public IQueryable<tbl_computerinfo_staging> Gettbl_computerinfo_staging()
        {
            return db.tbl_computerinfo_staging;
        }

        // GET: api/ComputerAdd/5
        [ResponseType(typeof(tbl_computerinfo_staging))]
        public IHttpActionResult Gettbl_computerinfo_staging(int id)
        {
            tbl_computerinfo_staging tbl_computerinfo_staging = db.tbl_computerinfo_staging.Find(id);
            if (tbl_computerinfo_staging == null)
            {
                return NotFound();
            }

            return Ok(tbl_computerinfo_staging);
        }

1 个答案:

答案 0 :(得分:1)

对此进行排序。显然在global.asax.cs中,WebApiConfig.Register需要放在RouteConfig.Register路由之上:

global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Http;

    namespace Inbound
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                GlobalConfiguration.Configure(WebApiConfig.Register);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    }
相关问题