同一域中的Web API 2项目和MVC 5网站项目 [英] Web API 2 project and MVC 5 Website project in same domain

查看:84
本文介绍了同一域中的Web API 2项目和MVC 5网站项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的技术:

  • BreezeJS
  • OData
  • Web API 2
  • MVC 5
  • IDE:Visual Studio 2013

我一直在努力在一个解决方案中拥有一个Web API项目和一个单独的网站项目的想法.

I've been wrestling with the idea of having a Web API project and a separate web site project in a single solution.

  • 我的Web API 2项目打开为:localhost:2020/ExampleProject.API
  • 我的MVC 5 WebSite项目打开为:localhost:5050/ExampleProject.WebSite

默认情况下,网络api现在不允许跨源策略.因此,我尝试在Web API 2中启用CORS,尽管我能够使其工作,但它仅适用于最新的浏览器.我需要IE7与IE9的向后兼容性.

Now by default web api doesn't allow cross origin policies. So I played around with enabling CORS in my Web API 2, although I was able to get it to work, it only works for the latest browsers; I need the backward compatibility of IE7 to IE9.

所以我玩了JSONP.我不喜欢缺乏对此的支持.我能够使其适用于Web API 2项目,但如果使用breezejs Web api库来使用BreezeController,则无法使用.如果我想创建一个ODataController,它也行不通.

So I played around with JSONP. I'm not fond of the lack of support that exists for this. I was able to get it to work for my Web API 2 project, but it doesn't work if I wanted to use BreezeController if using the breezejs web api library. It also doesn't work if I wanted to create an ODataController.

因此,我正在摆脱跨源共享的想法;尽管希望将来无论我使用BreezeJS WebAPI帮助程序还是ODataControllers,对jsonp都有足够的支持.

So I'm moving away from the idea of cross origin sharing; though hoping that in the future there will be enough support for jsonp regardless if I use BreezeJS WebAPI helper or ODataControllers.

就目前而言,我不知道如何将我的WebAPI项目和MVC 5网站放在我可以拥有的相同域下:

For now, I have no idea how to put my WebAPI project and my MVC 5 Website under the same domain where I can have:

  • localhost/ExampleProject.API
  • localhost/ExampleProject.WebSite

我必须在主机文件中进行一些配置吗?如果我想从VS2013运行我的项目,是否可以在同一个域下运行两个项目?还是必须继续在浏览器中手动更改URL?

Do I have to make some configuration in my host file? if I want to run my projects from VS2013 would it be able to run both projects under the same domain.. or do I have to keep on manually changing the URL in the browser?

推荐答案

嗯,答案真的很简单.

我知道这是一个古老的问题,只是因为我这样做已经很久了,所以我忘记了如何去做.在Google和stackoverflow上搜索答案很困难,因为讨论中讨论的是设置跨源策略,而不是设置相同的源策略.

I know this an old question, it was just that I forgot how to do it since it's been so long since I did this. Searching for the answers on google and on stackoverflow was difficult since the discussions talked about setting up cross origin policies instead of setting up a same origin policy.

我大部分时间都花在了IIS上.

I spent the good portion of my time putting everything on IIS.

  1. 创建了一个网站,并使其指向我的网站csproj和bin文件夹所在的物理路径.我给它提供了一个主机名"dev.example.com",并将我的主机文件更改为127.0.0.1以引用dev.example.com

  1. Created a website and had it point to the physical path where my website csproj and bin folder is located. I gave it a hostname of "dev.example.com" and changed my hostfile for 127.0.0.1 to refer to dev.example.com

为该网站创建了一个Web应用程序,并将其设置为Web api 2项目.在那之后,一切都神奇地起作用了.

Created a Web Application for the website, and set it up for web api 2 project. Everything magically worked after that.

愚蠢的部分是,我可以在Visual Studio中轻松完成此操作.我记得在VS2010中,这会引起很多问题,但是在2013年,我认为错误已得到修复,并且现在可以使用.

The dumb part was, I could have easily done this in visual studio. I remember in VS2010, this cause many problems, but in 2013, I guess bugs have been fixed and it works now.

我的WebSite项目设置为

I had my WebSite project set up as

localhost:2020/ExampleProject.WebSite(不正确)

localhost:2020/ExampleProject.WebSite (incorrect)

代替

localhost:2020(正确)

localhost:2020 (correct)

并创建一个虚拟目录.我可以通过右键单击该项目并转到"Web"选项卡下的属性来完成此操作.

and created a virtual directory. I do this by right clicking the project, going to properties, under "Web" tab.

因此,基本上,经验法则是让我的网站成为我的主要根域,并将该完整域复制到我想在其下添加的任何Web应用程序中.

So basically, the rule of thumb was to get my website to be my main root domain, and to copy that full domain to any web application I want to add under it.

因此:网站将是:

localhost:2020

localhost:2020

网络应用程序:

localhost:2020/ExampleProject.API

localhost:2020/ExampleProject.API

这篇关于同一域中的Web API 2项目和MVC 5网站项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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