![](/img/trans.png)
[英]Access to XMLHttpRequest at 'http://localhost:54111/RegisterController' from origin 'http://localhost:4200' has been blocked by CORS policy
[英]Access to XMLHttpRequest from origin 'http://localhost:4200' has been blocked by CORS policy (Angular, WebApi)
我想這個錯誤在這里是眾所周知的......我之前在當前項目中遇到過這個錯誤,當我試圖修復它時,他們說我缺少 EnableCors 的權限,即使它明確表示有(見項目下方) . 最后問題是屬性的位置,我不得不把它放在第一位,然后是 RoutePrefix 的屬性。 而且效果很好。 現在我又做了一個function,一試運行,這個錯誤又回來了。 我已經測試了很多解決這個問題的方法,但是我對這一切都很陌生,我有點難以理解我應該在哪里寫什么......我正在努力思考,也許它與我寫的新 function? 它可能有問題。 新的 function 是GetProductivityRequest 。
這是一個 WebApi 項目。
那是我的代碼:
用戶控制器.cs
using BL;
using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
namespace WebApiSystem.Controllers
{
//at first, the atributs looks like this:
//[RoutePrefix("api/User")]
//[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*", SupportsCredentials = true)]
//When I changed the position as you can see, the problem was solved.
[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*", SupportsCredentials = true)]
[RoutePrefix("api/User")]
public class UserController : ApiController
{
[HttpGet]
[Route("login/{email}/{pass}")]
public IHttpActionResult LogIn(string email, string pass)
{
UserDTO user = BL.UserService.LogIn(email, pass);
if (user != null)
return Ok(user);
return NotFound();
}
[Route("register")]
public IHttpActionResult Register(UserDTO user)
{
return NotFound();
}
[HttpPost]
[Route("productivity")]
public IHttpActionResult InsertProductivity([FromBody] ProductivityDTO p)
{
bool b = BL.UserService.InsertProductivity(p);
if (b)
return Ok(b);
return BadRequest();
}
[HttpGet]
[Route("getProductivityRequest")] //This is the function that brought back my problem
public IHttpActionResult GetProductivityRequest()
{
return Ok(UserService.GetProductivityRequest());
}
}
}
用戶服務.cs
using DAL;
using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BL
{
public class UserService
{
public static UserDTO LogIn(string email, string pass)
{
using (Model2 db = new Model2())
{
UserTbl user = db.UserTbl.FirstOrDefault(u => u.UserEmail == email && u.UserPassLogin == pass);
if (user == null)
return null;
return CONVERTERS.UserConverter.ConvertUsertoDTO(user);
}
}
public static bool InsertProductivity(ProductivityDTO p)
{
using (Model2 db = new Model2())
{
ProductivityTbl prod = BL.CONVERTERS.ProductivityConverter.ProductivityDal(p);
prod.ProductivityStatus = 1;
db.ProductivityTbl.Add(prod);
db.SaveChanges();
return true;
}
return false;
}
//This is the function that brought back my problem
public static IQueryable<dynamic> GetProductivityRequest()
{
using (Model2 db = new Model2())
{
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
IQueryable<dynamic> PList = db.ProductivityTbl.Select(p=> new { ProductivityStatus= 1 });
return PList;
}
}
}
}
Somone 告訴我,我可以在 web api 內的 Startup class 的 Configure() 方法中配置行為,但我不確定它是什么意思...
他告訴我寫這個:
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
app.UseCors("AllowAll");
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
}
我仍然認為問題可能是因為我寫的新 function。 如果你能看一下,也許我在那里寫錯了,這將對我有很大幫助。 謝謝!
我想這個錯誤在這里是眾所周知的......我之前在當前項目中遇到過這個錯誤,當我試圖修復它時,他們說我缺少 EnableCors 的權限,即使它明確表示有(見項目下方) . 最后問題是屬性的位置,我不得不把它放在第一位,然后是 RoutePrefix 的屬性。 而且效果很好。 現在我又做了一個function,一試運行,這個錯誤又回來了。 我已經測試了很多解決這個問題的方法,但是我對這一切都很陌生,我有點難以理解我應該在哪里寫什么......我正在努力思考,也許它與我寫的新 function? 它可能有問題。 新的 function 是GetProductivityRequest 。
這是一個 WebApi 項目。
那是我的代碼:
用戶控制器.cs
using BL;
using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
namespace WebApiSystem.Controllers
{
//at first, the atributs looks like this:
//[RoutePrefix("api/User")]
//[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*", SupportsCredentials = true)]
//When I changed the position as you can see, the problem was solved.
[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*", SupportsCredentials = true)]
[RoutePrefix("api/User")]
public class UserController : ApiController
{
[HttpGet]
[Route("login/{email}/{pass}")]
public IHttpActionResult LogIn(string email, string pass)
{
UserDTO user = BL.UserService.LogIn(email, pass);
if (user != null)
return Ok(user);
return NotFound();
}
[Route("register")]
public IHttpActionResult Register(UserDTO user)
{
return NotFound();
}
[HttpPost]
[Route("productivity")]
public IHttpActionResult InsertProductivity([FromBody] ProductivityDTO p)
{
bool b = BL.UserService.InsertProductivity(p);
if (b)
return Ok(b);
return BadRequest();
}
[HttpGet]
[Route("getProductivityRequest")] //This is the function that brought back my problem
public IHttpActionResult GetProductivityRequest()
{
return Ok(UserService.GetProductivityRequest());
}
}
}
用戶服務.cs
using DAL;
using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BL
{
public class UserService
{
public static UserDTO LogIn(string email, string pass)
{
using (Model2 db = new Model2())
{
UserTbl user = db.UserTbl.FirstOrDefault(u => u.UserEmail == email && u.UserPassLogin == pass);
if (user == null)
return null;
return CONVERTERS.UserConverter.ConvertUsertoDTO(user);
}
}
public static bool InsertProductivity(ProductivityDTO p)
{
using (Model2 db = new Model2())
{
ProductivityTbl prod = BL.CONVERTERS.ProductivityConverter.ProductivityDal(p);
prod.ProductivityStatus = 1;
db.ProductivityTbl.Add(prod);
db.SaveChanges();
return true;
}
return false;
}
//This is the function that brought back my problem
public static IQueryable<dynamic> GetProductivityRequest()
{
using (Model2 db = new Model2())
{
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
IQueryable<dynamic> PList = db.ProductivityTbl.Select(p=> new { ProductivityStatus= 1 });
return PList;
}
}
}
}
Somone 告訴我,我可以在 web api 內的 Startup class 的 Configure() 方法中配置行為,但我不確定它是什么意思...
他告訴我寫這個:
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
app.UseCors("AllowAll");
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
}
我仍然認為問題可能是因為我寫的新 function。 如果你能看一下,也許我在那里寫錯了,這將對我有很大幫助。 謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.