C# 如何向asp.net中的context.response添加新标题
我对asp.net很陌生。我正在构建一个现有的项目,其中来自js文件的用户名和密码被加密,并且对.aspx.cs文件进行ajax函数调用。这个ashx.cs文件对它们进行解密,然后将它们插入到数据库表中。现在我想加入一个名为“域”的新字段,但我不知道如何为它创建一个头,以便将它从js传递到ashx.cs文件。我的问题可能很傻,但我已经为此挣扎了一段时间,非常感谢您的帮助。我尝试对域使用context.Response.AddHeader,但我不知道应该向其中传递什么值 “RUN”和“RUP”似乎已经在某个地方创建了,我无法找到它们来遵循相同的域标题 js文件:C# 如何向asp.net中的context.response添加新标题,c#,asp.net,ajax,httpresponse,C#,Asp.net,Ajax,Httpresponse,我对asp.net很陌生。我正在构建一个现有的项目,其中来自js文件的用户名和密码被加密,并且对.aspx.cs文件进行ajax函数调用。这个ashx.cs文件对它们进行解密,然后将它们插入到数据库表中。现在我想加入一个名为“域”的新字段,但我不知道如何为它创建一个头,以便将它从js传递到ashx.cs文件。我的问题可能很傻,但我已经为此挣扎了一段时间,非常感谢您的帮助。我尝试对域使用context.Response.AddHeader,但我不知道应该向其中传递什么值 “RUN”和“RUP”似乎
function Register() {
if (ValidateRegisterWindow()) {
var URL = "SecureChatServer.ashx";
if (URL == null) { alert("Request URL is Empty"); }
else {
username = Encrypt(document.getElementById('NewUserName').value, EncryptionKey);
password = Encrypt(document.getElementById('NewPassword').value, EncryptionKey);
domain = Encrypt(document.getElementById('NewDomain').value, EncryptionKey);
AjaxRequest(ProcessRegisterResponse, URL, "POST", '', '', { RequestCode: 'SC006', RUN: username, RUP: password}); //how to pass domain here??
}
}
}
function ProcessRegisterResponse() {
var ResponseStatus = GetHeader(ResponseHeaderJSON, 'ResponseStatus');
if (ResponseStatus == "RS-OK") {
ShowAlertMessage("Registration Sucessful", "", "User Registered and Logged in Sucessfully");
CurrentUser = document.getElementById('NewUserName').value;
LoginEvents(CurrentUser, true);
ClearRegisterWindow();
}
else if (ResponseStatus == "RS-Failed") {
ShowErrorBox("Registration Error", "This username cannot be registered ,please try a different username");
}
else {
ShowErrorBox("Unknown Error :Code-01 " + ResponseStatus, "Request cannot be processed ,please try again.");
}
}
ashx.cs文件:
#region Handle Add New User Request
case "SC006": //indicates request to add new user
{
string UserName, Password;
UserName = Decrypt(context.Request.Headers["RUN"], EncryptionKey);
Password = Decrypt(context.Request.Headers["RUP"], EncryptionKey);
Users newuser = new Users();
try
{
if (newuser.AddUser(UserName, Password, SessionID, UserIPAddress))
{
context.Response.AddHeader("CustomHeaderJSON", "ResponseStatus:'RS-OK'");
}
else
{
context.Response.AddHeader("CustomHeaderJSON", "ResponseStatus:'RS-Failed'");
}
}
catch (Exception e)
{
Debug.WriteLine("Failed Request SC006 : " + e.ToString());
context.Response.AddHeader("CustomHeaderJSON", "ResponseStatus:'RS-Exception'");
}
}
break;
#endregion
您可以使用此代码轻松地添加标题
context.Response.Headers.Add(“此处为您的标题”) .NET framework 3.5是的,我试过了,但是我不知道要为NameValueCollection传递什么值。你能再解释一下吗?谢谢,我创建了一个新的NameValueCollection并传递了它。它起作用了System.Collections.Specialized.NameValueCollection=new System.Collections.Specialized.NameValueCollection();context.Response.Headers.Add(RUT)`