c# – 服务有时仅抛出No’Access-Control-Allow-Origin’标头错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 服务有时仅抛出No’Access-Control-Allow-Origin’标头错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6054字,纯文字阅读大概需要9分钟。
内容图文
我有一个服务有几种方法.我设置我的Global.asax文件来处理CORS,并且所有东西都工作得很好,包括CORS ……直到我在接口文件中添加了以下内容:
[OperationContract]
[WebInvoke(UriTemplate = "/Transform", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
bool XXXTransform(string user, string x);
以及服务类的以下内容……
bool XXXTransform(string user, string x)
{
return true;
}
现在,这是非常有趣的事情.当我调用GetRecipes方法时发生错误.更有趣的是,如果我只为接口和服务类的XXXTransform指定了一个参数,它就不会抛出错误 – 如bool XXXTransform(string user)
有没有人见过这样的事情呢?为什么使用一个参数可以工作,但是在调用完全不同的方法时,两个参数会导致原始错误?
IChomp.cs:
namespace NomNomNotebookService
{
// May need to mod service behavior for upload...
[ServiceContract]
public interface IChomp
{
[OperationContract]
[WebInvoke(UriTemplate = "/{userId}/Recipes", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
List<Recipe> GetRecipes(string userId);
[OperationContract]
[WebInvoke(UriTemplate = "/{userId}/Recipes", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Recipe AddRecipe(string userId, Recipe recipe);
[OperationContract]
[WebInvoke(UriTemplate = "/{userId}/Recipes/{recipeId}", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Recipe UpdateRecipe(string userId, string recipeId, Recipe recipe);
[OperationContract]
[WebInvoke(UriTemplate = "/{userId}/Recipes/{recipeId}", Method = "DELETE", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
bool DeleteRecipe(string userId, string recipeId);
[OperationContract]
[WebInvoke(UriTemplate = "/{userId}/Recipes/{recipeId}", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
Recipe GetRecipe(string userId, string recipeId);
[OperationContract]
[WebInvoke(UriTemplate = "/Categories", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
List<Category> GetCategories();
[OperationContract]
[WebInvoke(UriTemplate = "/Upload", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
bool XXXUpload();
[OperationContract]
[WebInvoke(UriTemplate = "/Transform", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
bool XXXTransform(string user, string x); // WILL work if I have bool XXXTransform(string user)
}
}
Chomp.cs
namespace NomNomNotebookService
{
public class Chomp : IChomp
{
private DBConnector _dbCon = new DBConnector();
public List<Recipe> GetRecipes(string userId)
{
_dbCon.Parameters.Add(new CustSqlParam("@userId", Int16.Parse(userId)));
DataTable dt = _dbCon.RunProc("J_NNN_GetRecipesForUser");
List<Recipe> recipes = new List<Recipe>();
foreach (DataRow row in dt.Rows)
{
recipes.Add(new Recipe((int)row["Uid"], (string)row["Name"], (string)row["Description"], (string)row["Category"]));
}
_dbCon.Kill();
return recipes;
}
public Recipe GetRecipe(string userId, string recipeId)
{
_dbCon.Parameters.Add(new CustSqlParam("@uid", Int16.Parse(recipeId)));
DataTable dt = _dbCon.RunProc("J_NNN_GetRecipe");
Recipe recipe = new Recipe(dt.Rows[0]);
_dbCon.Kill();
return recipe;
}
// USER ID IS HERE FOR VALIDATION ONCE SESSIONS ARE ROLLING
public Recipe AddRecipe(string userId, Recipe recipe)
{
_dbCon.Parameters.Add(new CustSqlParam("@userId", Int16.Parse(userId)));
_dbCon.Parameters.Add(new CustSqlParam("@name", recipe.Name));
_dbCon.Parameters.Add(new CustSqlParam("@description", recipe.Description));
_dbCon.Parameters.Add(new CustSqlParam("@categoryId", recipe.Category.Uid));
_dbCon.Parameters.Add(new CustSqlParam("@prepTime", recipe.PrepTime));
_dbCon.Parameters.Add(new CustSqlParam("@cookTime", recipe.CookTime));
_dbCon.Parameters.Add(new CustSqlParam("@readyTime", recipe.ReadyTime));
DataTable dt = _dbCon.RunProc("J_NNN_AddRecipe");
_dbCon.Kill();
return new Recipe(dt.Rows[0]);
}
public Recipe UpdateRecipe(string userId, string recipeId, Recipe recipe)
{
_dbCon.Parameters.Add(new CustSqlParam("@uid", Int16.Parse(recipeId)));
_dbCon.Parameters.Add(new CustSqlParam("@name", recipe.Name));
_dbCon.Parameters.Add(new CustSqlParam("@description", recipe.Description));
_dbCon.Parameters.Add(new CustSqlParam("@categoryId", recipe.Category.Uid));
_dbCon.Parameters.Add(new CustSqlParam("@prepTime", recipe.PrepTime));
_dbCon.Parameters.Add(new CustSqlParam("@cookTime", recipe.CookTime));
_dbCon.Parameters.Add(new CustSqlParam("@readyTime", recipe.ReadyTime));
DataTable dt = _dbCon.RunProc("J_NNN_UpdateRecipe");
_dbCon.Kill();
return new Recipe(dt.Rows[0]);
}
public bool DeleteRecipe(string userId, string recipeId)
{
try
{
_dbCon.Parameters.Add(new CustSqlParam("@uid", Int16.Parse(recipeId)));
DataTable dt = _dbCon.RunProc("J_NNN_DeleteRecipe");
_dbCon.Kill();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
return true;
}
public List<Category> GetCategories()
{
DataTable dt = _dbCon.RunProc("J_NNN_GetCategories");
_dbCon.Kill();
List<Category> categories = new List<Category>();
foreach (DataRow row in dt.Rows)
{
categories.Add(new Category((int)row["Uid"], (string)row["Name"]));
}
return categories;
}
// Upload shit....
public bool XXXUpload()
{
return false;
}
public bool XXXTransform(string user, string x) // WILL work if I have bool XXXTransform(string user)
{
return true;
}
}
}
Global.asax中
namespace NomNomNotebookService
{
public class Global : System.Web.HttpApplication
{
protected void Session_Start(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
}
}
解决方法:
我过去也遇到过类似的错误.您是否在配置中设置了请求标头?
'Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'
'Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'
内容总结
以上是互联网集市为您收集整理的c# – 服务有时仅抛出No’Access-Control-Allow-Origin’标头错误全部内容,希望文章能够帮你解决c# – 服务有时仅抛出No’Access-Control-Allow-Origin’标头错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。