简体   繁体   English

如何在IIS中托管webAPI / HTML?

[英]How to Host webAPI /HTML in IIS?

i have created a project in VS2015,My architecture goes like this,using ASP.NET empty website i have added few html,jquery,highcharts etc(not used any aspx page) and for same solution using webApi nuget packages i added controllers and added class libraries(Business layer,Data layer) to same project.so from html page using AGAX i am calling webapi(exsits in the same soloution) and controller methods will contact data layer to fetch data, i will get json and i will bind to HTML and i am using Token based authentication,everything is working perfectly. 我在VS2015中创建了一个项目,我的体系结构是这样的,使用ASP.NET空网站我添加了一些html,jquery,highcharts等(未使用任何aspx页面),并且对于使用webApi nuget包的相同解决方案,我添加了控制器并添加了类库(业务层,数据层)到相同的project.so,使用AGAX从html页面开始,我正在调用webapi(存在于同一解决方案中),并且控制器方法将与数据层联系以获取数据,我将获取json并且绑定到HTML和我正在使用基于令牌的身份验证,一切工作正常。

Now the problem is how to HOST this solution in IIS,Initially what i did, first i published my solution API part and hosted in IIS(8081 port) and able to call webapi. 现在的问题是如何在IIS中托管此解决方案。最初,我首先发布了解决方案API部分,并将其托管在IIS(8081端口)中并能够调用webapi。 then i added my HTML(UI) part to IIS(8080 port) and able to browse the page , but while calling WEB API it leads to HTML CROSS problem, because WEB API is in Different Domain, so with few changes in WEBAPI i am able solve HTML CROSS problem but still few more challenges, 然后我将HTML(UI)部分添加到IIS(8080端口)并能够浏览页面,但是在调用WEB API时会导致HTML CROSS问题,因为WEB API在不同的域中,所以我在WEBAPI中所做的更改很少能够解决HTML CROSS问题,但还有更多挑战,

So is there any best solution for this, i am a mid level developer till now i haven't done any proper deployments.Can we host everything in one domian? 所以有什么最佳解决方案吗?我是中级开发人员,直到现在我还没有做任何适当的部署。我们可以将所有内容都托管在一个domian中吗? solution reference: http://www.codeproject.com/Articles/815916/Create-an-ASP-NET-Web-Forms-Application-using-Boot 解决方案参考: http : //www.codeproject.com/Articles/815916/Create-an-ASP-NET-Web-Forms-Application-using-Boot 在此处输入图片说明

add the following to your webconfig of your api project 将以下内容添加到您的api项目的webconfig中

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

This will allow your api's to communicate with all domains. 这将允许您的api与所有域进行通信。

For doing everything in a single domain i would prefer you to bring all html files in to razor.Then from there you can controll the html via simple ajax calls.So it will look like a neat SPA. 为了在单个域中完成所有操作,我希望您将所有html文件都放入剃刀中,然后从那里可以通过简单的ajax调用来控制html,因此它看起来像是一个简洁的SPA。

避免CORS问题的最佳方法是首先发布您的WEB API,然后在IIS中添加为该应用程序的新应用程序并添加HTML网站,因为两者都位于IIS的同一端口中,所以我们可以避免CORS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM