的getJSON网页API开发工作的服务器上,但IIS防爆$ P $ 7.5 PSS投404未找​​到 [英] getJSON Web API works on Development Server but IIS Express 7.5 throws 404 Not found

查看:216
本文介绍了的getJSON网页API开发工作的服务器上,但IIS防爆$ P $ 7.5 PSS投404未找​​到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Visual Studio 2012和ASP.NET开发服务器一切似乎我的第一个进军ASP.NET中运行良好。使用jQuery我的Web API调用返回的数据。在以开始学习认证/授权部署到IIS防爆$ P $ 7.5 PSS我遇到我的getJSON呼叫的障碍:

  GET HTTP://本地主机/ API / OSList 404(未找到)

WebApiConfig

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

的getJSON

  $。的getJSON(/ API / OSList,NULL)
    。成功(
        功能(数据){
            VAR mappedOs = $ .MAP(数据,功能(项目){
                返回新的操作系统(item.ID,item.Name,item.PatchLevel);
            });
            self.OSs(mappedOs);
        }
    );

这所有的努力开发服务器上...有什么事情我还没有在IIS中设置正确,将阻止呢?

编辑:新增控制器code,这是对我而言有明显的怀念。这是默认的开发工作室自动生成。

 公共类OSListController:ApiController
{
    私人只读TrendDataEntities _db =新TrendDataEntities();    //获取API / OSList
    公共IEnumerable的< OS> GetOS()
    {
        返回_db.OS.AsEnumerable();
    }
}

和HomeController的用途:

 公众的ActionResult OSList()
{
    ViewBag.Title =操作系统列表;
    ViewBag.Message =;
    返回视图(〜/查看/首页/管理/ OSList.cshtml);
}


解决方案

Visual Studio开发服务器上运行您的特定端口上的应用程序,使的http://本地主机:<港口> / API / OSList 将工作。如果应用程序部署到IIS,你的应用程序并不以及其他应用的专用端口,但80(默认)上运行。在这种情况下,你的URI必须包含您的应用程序的名称为好。所以,如果你的应用程序名称为MyWebApi,那么你的URI必须的http://本地主机/ mywebapi / API / OSList 。的getJSON必须像 $。的getJSON(/ mywebapi / API / OSList,NULL)

Using Visual Studio 2012 and its ASP.NET Development Server everything seemed to be running well for my first forays into ASP.NET. My Web API calls using jquery returned data. In deploying to IIS Express 7.5 in order to start learning Authentication/Authorization I am running into a roadblock in my getJSON call:

GET http://localhost/api/OSList 404 (Not Found)

WebApiConfig

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

getJSON

$.getJSON("/api/OSList", null)
    .success(
        function(data) {
            var mappedOs = $.map(data, function(item) {
                return new Os(item.ID, item.Name, item.PatchLevel);
            });
            self.OSs(mappedOs);
        }
    );

This all worked on Development Server... Is it something I have not set up correctly in IIS that would block this?

Edit: Added controller code, that was an obvious miss on my part. This is default dev studio autogenerated.

public class OSListController : ApiController
{
    private readonly TrendDataEntities _db = new TrendDataEntities();

    // GET api/OSList
    public IEnumerable<OS> GetOS()
    {
        return _db.OS.AsEnumerable();
    }
}

And the HomeController uses:

public ActionResult OSList()
{
    ViewBag.Title = "OS List";
    ViewBag.Message = "";
    return View("~/Views/Home/Admin/OSList.cshtml");
}

解决方案

Visual Studio Development Server runs your application on a specific port so that http://localhost:<port>/api/OSList will work. If you deploy your application to IIS, your application does not run on a dedicated port but 80 (by default) along with other apps. In that case, your URI must include your application name as well. So, if your application name is MyWebApi, then your URI must be http://localhost/mywebapi/api/OSList. getJSON must be something like $.getJSON("/mywebapi/api/OSList", null).

这篇关于的getJSON网页API开发工作的服务器上,但IIS防爆$ P $ 7.5 PSS投404未找​​到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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