ASP.NET C#中带有http状态代码的Json api输出参数 [英] Json api output parameter with http status code in ASP.NET C#
本文介绍了ASP.NET C#中带有http状态代码的Json api输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用asp.net c#在json中开发web api。我希望http状态代码带有输出参数以响应api的。我可以实现这一点。
我尝试过:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetUserLogin( string 合同, string username
{
Authentication obj = new Authentication();
字符串 jSonString = ;
System.Web.Script .Serialization.JavaScriptSerializer serializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
SqlConnection conn = new SqlConn ection(System.Configuration.ConfigurationManager.AppSettings [ conststring]);
SqlCommand command = new SqlCommand();
try
{
DataSet ds = new DataSet();
ds = obj.CheckContract(Contract);
if (ds.Tables [ 0 ]!= null && ds.Tables [ 0 ]。Rows.Count > ; 0 )
{
DataSet dscustomer = new DataSet( );
dscustomer = obj.CheckCustomer(ds.Tables [ 0 ]。行[ 0 ] [ 客户]。ToString(),用户名
if (dscustomer.Tables [ 0 ]!= null & ;& dscustomer.Tables [ 0 ]。Rows.Count > 0 )
{
string success = ;
success = {\Customer \:\ + ds.Tables [ 0 ]。行[ 0 跨度>] [ Customer]。ToString()+ \};
this .Context.Response.ContentType = application / json; charset = utf-8;
this .Context.Response.Write(success);
}
else
{
string success = ;
success = {\status \:\无效的登录详情。\ }跨度>;
this .Context.Response.ContentType = application / json; charset = utf-8;
this .Context.Response.Write(success);
}
}
else
{
string success = ;
success = {\status \:\无效的登录详情。\ }跨度>;
this .Context.Response.ContentType = application / json; charset = utf-8;
this .Context.Response.Write(success);
}
}
catch (例外)
{
string success = ;
success = {\status \:\ + er .Message + \};
this .Context.Response.ContentType = application / json; charset = utf-8;
this .Context.Response.Write(success);
}
最后
{
command.Dispose();
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
}
解决方案
我用google搜索asp.net webmethod set status code为你而且这里有一些结果
asp.net - 我可以设置HTTP响应代码&在ASMX JSON服务上抛出异常? - 堆栈溢出 [ ^ ]
c# - Asp.Net web服务:我想返回错误403 forbidden - Stack Overflow [ ^ ]
您可以自己轻松完成此操作,请在提问之前进行基础研究。
I am developing web api's in json using asp.net c#. I want http status code with output parameters in response of api's .how I can I achieve this.
What I have tried:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetUserLogin(string Contract, string username
{
Authentication obj = new Authentication();
String jSonString = "";
System.Web.Script.Serialization.JavaScriptSerializer serializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conststring"]);
SqlCommand command = new SqlCommand();
try
{
DataSet ds = new DataSet();
ds = obj.CheckContract(Contract);
if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
{
DataSet dscustomer = new DataSet();
dscustomer = obj.CheckCustomer(ds.Tables[0].Rows[0]["Customer"].ToString(), username
if (dscustomer.Tables[0] != null && dscustomer.Tables[0].Rows.Count > 0)
{
string success = "";
success = "{\"Customer\": \" " + ds.Tables[0].Rows[0]["Customer"].ToString() + " \"}";
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(success);
}
else
{
string success = "";
success = "{\"status\": \"Invalid Login Details.\"}";
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(success);
}
}
else
{
string success = "";
success = "{\"status\": \"Invalid Login Details.\"}";
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(success);
}
}
catch (Exception er)
{
string success = "";
success = "{\"status\": \"" + er.Message + "\"}";
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(success);
}
finally
{
command.Dispose();
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
}
解决方案
I googled "asp.net webmethod set status code" for you and here are some results
asp.net - Can I Set the HTTP Response Code & Throw an Exception on an ASMX JSON Service? - Stack Overflow[^]
c# - Asp.Net web service: I would like to return error 403 forbidden - Stack Overflow[^]
You could easily have done this yourself, please do basic research before asking a question.
这篇关于ASP.NET C#中带有http状态代码的Json api输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文