解决asp.net mvc的跨域请求问题

时间:2022-08-29 12:23:50

解决asp.net mvc的跨域请求问题


web.config中配置如下内容:

123456789101112131415 <system.webServer>    <httpProtocol>        <customHeaders>            <add name="Access-Control-Allow-Origin" value="*" />            <add name="Access-Control-Allow-Headers" value="Content-Type" />            <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />        </customHeaders>    </httpProtocol>    <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>

 

如果是webapi,还需要在每个controller中添加一个方法:

1234 public string Options(){    return null;}