Asp.net 使用IIS Express在Visual Studio中运行Angular和Web API

Asp.net 使用IIS Express在Visual Studio中运行Angular和Web API,asp.net,visual-studio,iis-express,Asp.net,Visual Studio,Iis Express,我有一个Visual Studio 2013解决方案,其中包含一个Web API项目和一个Web UI项目(使用Angular)。我正在使用IIS Express 有没有一种方法可以设置这些项目,以便Angular代码可以调用Web API项目,而无需在本地主机和端口号中进行硬编码 return $http.get("http://localhost:1561/api/products") .then(function (response)

我有一个Visual Studio 2013解决方案,其中包含一个Web API项目和一个Web UI项目(使用Angular)。我正在使用IIS Express

有没有一种方法可以设置这些项目,以便Angular代码可以调用Web API项目,而无需在本地主机和端口号中进行硬编码

            return $http.get("http://localhost:1561/api/products")
                .then(function (response) {
                    return response.data;
                });
如果我硬编码localhost:1561而不是仅仅使用“/api/products”样式,那么我必须在部署到生产环境之前手动更改代码,并将其更改回开发过程中运行

有没有更简单的方法


谢谢

我实际上编写了一个简单的实用程序方法,它返回绝对URL,因此在代码中只需键入相对URL即可。下面是这个方法,您应该能够通过传递相对URL(即/api/products)或将其转换为助手扩展方法从JS调用它

var rootPath;

if (location.hostname === 'localhost') {
    rootPath = 'http://localhost:1561';
} else {
    rootPath = 'http://' + location.hostname;
}

return $http.get(rootPath + '/api/products')
    .then(function (response) {
        return response.data;
    });
// Code
public static string ToAbsoluteUrl(string relativeUrl)
        {
            if (string.IsNullOrEmpty(relativeUrl))
                return relativeUrl;

            if (HttpContext.Current == null)
                return relativeUrl;

            if (relativeUrl.StartsWith("/"))
                relativeUrl = relativeUrl.Insert(0, "~");
            if (!relativeUrl.StartsWith("~/"))
                relativeUrl = relativeUrl.Insert(0, "~/");

            var url = HttpContext.Current.Request.Url;
            var port = url.Port != 80 ? (":" + url.Port) : String.Empty;

            return String.Format("{0}://{1}{2}{3}",
                url.Scheme, url.Host, port, VirtualPathUtility.ToAbsolute(relativeUrl));
        }

据我所知,我不可能做我想做的事。以下是选项:

1) 在本地计算机上使用IIS。这样,您可以根据需要设置路径/虚拟目录。这也使得在调试期间从其他浏览器调用API变得更容易。注意:这确实需要从现在开始以管理模式运行VisualStudio

2) 把这两个项目放在一起。对我来说,这不是一个有效的选项,因为我需要交付完全独立于API代码的UI代码


希望这对其他尝试使用Angular和Web API的人有所帮助。

我假设此解决方案不适用于在系统上使用IIS的团队成员,因为它将尝试使用我的IIS Express端口号?仅供参考。。。Angular代码是通过以下URL从IIS Express执行的:因此,我不认为可以使用客户端的位置来查找Web API服务器。我开始觉得IIS Express不适合这个任务。谢谢你的建议。这个代码到哪里去了?我的整个UI都是棱角分明的,所以我没有任何地方可以调用C#代码。