设置 WebAPI,Fiddler 响应是一个 html 页面

Setting up a WebAPI, Fiddler response is an html page

我正在尝试建立一个将使用 RESTful 服务的网络 api。我正在关注本指南。

Getting Started with ASP.NET Web API 2 (C#)

我也在按照本指南进行设置 Entity Framework。

Getting Started with Entity Framework 6 Code First using MVC 5

当我在 Fiddler 中 运行 一个 Composer。我得到了 Home.aspx

的网页

这是我的控制器的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebServer.z_Repository;
using WebServer.z_Models;

namespace WebServer.z_Controllers
{
    [Route("api/Locations")]
    public class LocationsController : ApiController
    {
        // GET api/<controller>
        static IlocationsRepository LocationsRepo;

        public LocationsController(IlocationsRepository _repo)
        {
            if (_repo == null) { throw new ArgumentNullException("_repo"); }
            LocationsRepo = _repo;
        }

        [HttpGet]
        public IEnumerable<Location> GetAll()
        {
            return LocationsRepo.GetAll();
        }
    }
}

我在 GetAll() 上设置了一个断点,但从未命中断点。这告诉我控制器没有在某处注册。但是指南没有说明应该在哪里注册。

我创建了一个 Global.asax.cs 页面,尽管这不在指南中。但是我不确定从这里去哪里。

Global.asax.cs

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace WebServer
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }
    }
}

这是显示 Fiddler 中响应的片段

主页显示是因为根据您在 Fiddler 中显示的 URL:

GET Home.aspx/api/locations

它被告知去 Home.aspx

您正在使用属性路由,但尚未显示任何设置。

参考:Attribute Routing in ASP.NET Web API 2

你的控制器应该是:

[RoutePrefix("api/Locations")]
public class LocationsController : ApiController
{
    IlocationsRepository locationsRepo;

    public LocationsController(IlocationsRepository _repo)
    {
        if (_repo == null) { throw new ArgumentNullException("_repo"); }
        this.locationsRepo = _repo;
    }

    //GET api/locations
    [HttpGet]
    [Route(""}]
    public IEnumerable<Location> GetAll()
    {
        return locationsRepo.GetAll();
    }
}

你的WebApiConfig.cs:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

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

并在您的全局中包含

protected void Application_Start()
{
    // Pass a delegate to the Configure method.
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

现在,为了访问网络位置 api,您需要致电

GET api/location
Host: localhost:59104

结果为 http://localhost:59104/api/locations