无法从同一网络上的其他计算机访问Web应用程序

时间:2018-03-16 12:25:21

标签: asp.net-web-api cors http-error

我在服务器上的IIS中部署了一个ASP .Net Web应用程序,当我在服务器上的浏览器中打开它时工作正常但是从任何其他机器我得到错误 " HTTP错误404.0 - 未找到 您要查找的资源已被删除,名称已更改或暂时不可用。"

我安装了IIS 8.5版,并已授予所有用户权限。请指导如何解决此问题。

url我想得到的回应是: http://serverName/serviceName/api/controller

应用程序的配置文件如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="username" value="username"/>
<add key="password" value="pwd"/>
<add key="kibanaurl" value="http://serverip:877"/>
<add key="elasticurl" value="http://serverip:9200/ams/logs/"/> 
</appSettings>
<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
  </httpProtocol>
</system.webServer>  

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>

<system.web>
<compilation targetFramework="4.5.1"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
<connectionStrings>
<add name="LogsData" connectionString="data source=server;initial 
    catalog=NVR_AMS;persist security 
info=True;user 
id=my_id;password=pwd;MultipleActiveResultSets=True;" 
 providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.webServer>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
  <remove name="OPTIONSVerbHandler"/>
  <remove name="TRACEVerbHandler"/>
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" 
type="System.Web.Handlers.TransferRequestHandler" 
preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
</system.webServer> <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Http" 
publicKeyToken="31BF3856AD364E35" culture="neutral"/>
            <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" 
newVersion="5.2.4.0"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Net.Http.Formatting" 
publicKeyToken="31BF3856AD364E35" culture="neutral"/>
            <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" 
newVersion="5.2.4.0"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>
</configuration>

当我的应用程序尝试调用post方法的服务时,它会收到CORS错误 虽然这在代码中得到了妥善处理。

允许CORS我在webapiconfig.cs类中添加了以下代码

    private static string GetAllowedOrigins()
    {
        string cs = 
        ConfigurationManager.ConnectionStrings["LogsData"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("NVR.spGetAllowedOrigins", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            using (IDataReader reader = cmd.ExecuteReader())
            {
                string urls = "";
                while (reader.Read())
                {
                    urls=urls+reader.GetString(0)+",";
                }
                urls = urls.Remove(urls.Length - 1);
                return urls;
            }
        }
    }
    public static void Register(HttpConfiguration config)
    {
        string origins = GetAllowedOrigins();
        var cors = new EnableCorsAttribute(origins, "*", "*");

        log4net.Config.XmlConfigurator.Configure();

        config.EnableCors(cors);
        config.MapHttpAttributeRoutes();

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

0 个答案:

没有答案