C# Asp.net代码未作为本地主机在实际服务器上执行
我有以下代码,使用IIS7在本地主机上运行良好,但当我将其上载到服务器时,其行为与在本地主机上的行为不同 例如,单击“提交”按钮时应检查它在本地主机上执行的验证,如果验证正确,则会重定向到URL 但是当在这里的实际服务器上尝试同样的方法时 当我引入一个重定向url(在C#代码中是Response.redirect(“http://www.google.com)在c#代码中…如果有更好的方法来实现这一点,那么所有这些麻烦都是不必要的..tks 这是我使用的代码C# Asp.net代码未作为本地主机在实际服务器上执行,c#,asp.net,C#,Asp.net,我有以下代码,使用IIS7在本地主机上运行良好,但当我将其上载到服务器时,其行为与在本地主机上的行为不同 例如,单击“提交”按钮时应检查它在本地主机上执行的验证,如果验证正确,则会重定向到URL 但是当在这里的实际服务器上尝试同样的方法时 当我引入一个重定向url(在C#代码中是Response.redirect(“http://www.google.com)在c#代码中…如果有更好的方法来实现这一点,那么所有这些麻烦都是不必要的..tks 这是我使用的代码 <%@ Page Langu
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
protected void btnSave_Click(object sender, EventArgs e)
{
txtAddress.Text = "";
string xmlPath = MapPath("Books.xml");
XmlDocument doc = new XmlDocument();
//Check if the file already exists or not
if (System.IO.File.Exists(xmlPath))
{
doc.Load(xmlPath);
XmlNode bookNode = CreateBookNode(doc);
//Get reference to the book node and append the book node to it
XmlNode bookStoreNode = doc.SelectSingleNode("bookstore");
bookStoreNode.AppendChild(bookNode);
lblResult.Text = "XML Document has been successfully updated";
txtAddress.Text = ""; Response.Redirect("http://www.google.com");
}
else
{
XmlNode declarationNode = doc.CreateXmlDeclaration("1.0", "", "");
doc.AppendChild(declarationNode);
XmlNode comment = doc.CreateComment("This file represents a fragment of a book store inventory database");
doc.AppendChild(comment);
XmlNode bookstoreNode = doc.CreateElement("bookstore");
XmlNode bookNode = CreateBookNode(doc);
//Append the book node to the bookstore node
bookstoreNode.AppendChild(bookNode);
//Append the bookstore node to the document
doc.AppendChild(bookstoreNode);
lblResult.Text = "XML Document has been successfully created";
txtAddress.Text = "";Response.Redirect("http://www.google.com");
}
doc.Save(xmlPath);
}
XmlNode CreateBookNode(XmlDocument doc)
{
/*
XmlNode bookNode = doc.CreateElement("book");
//Add the genre attribute to the book node
XmlAttribute genreAttribute = doc.CreateAttribute("genre");
genreAttribute.Value = txtGenre.Text;
bookNode.Attributes.Append(genreAttribute);
http://www.java2s.com/Code/ASP/XML/SaveformdatatoXMLfile.htm
*/
XmlNode bookNode = doc.CreateElement("book");
//Declaration of the Main Node (Particulars)
XmlNode particularsnode = doc.CreateElement("Particulars");
//Declaration of Child Nodes in the Main Node(Particulars)
XmlNode nameNode = doc.CreateElement("Name");
XmlNode phoneNode = doc.CreateElement("Phone");
XmlNode emailNode = doc.CreateElement("Email");
XmlNode AddressNode = doc.CreateElement("Address");
//Getting the textvalue from the htmlform
nameNode.InnerText = txtName.Text;
phoneNode.InnerText = txtPhone.Text;
emailNode.InnerText = txtEmail.Text;
AddressNode.InnerText = txtAddress.Text;
//Updating the XML file here the particularsnode has various children and they are being updated
particularsnode.AppendChild(nameNode);
particularsnode.AppendChild(phoneNode);
particularsnode.AppendChild(emailNode);
particularsnode.AppendChild(AddressNode);
bookNode.AppendChild(particularsnode);
//Declaration of the Main Node (BookParticulars)
XmlNode bookparticularsnode = doc.CreateElement("BookParticulars");
//Declaration of Child Nodes in the Main Node(BookParticulars)
XmlNode schoolNode = doc.CreateElement("School");
XmlNode currentlevelNode = doc.CreateElement("CurrentLevel");
XmlNode GABDNode = doc.CreateElement("GiveAwayBookDetails");
XmlNode LRNode = doc.CreateElement("LevelRequired");
//Getting the textvalue from the htmlform
schoolNode.InnerText = txtSchool.Text;
currentlevelNode.InnerText = txtCurrentLevel.Text;
GABDNode.InnerText = txtGABD.Text;
LRNode.InnerText = txtLR.Text;
//Updating the XML file here the particularsnode has various children and they are being updated
particularsnode.AppendChild(schoolNode);
particularsnode.AppendChild(currentlevelNode);
particularsnode.AppendChild(GABDNode);
particularsnode.AppendChild(LRNode);
bookNode.AppendChild(bookparticularsnode);
return bookNode;
}
public static string NewLineToBreak(string input)
{
Regex regEx = new Regex(@"[\n|\r]+");
return regEx.Replace(input, "<br />");
}
protected void txtAddress_Load(object sender, EventArgs e)
{
txtAddress.Text = "Woodlands Drive 14\n Blk";
}
</script>
受保护的无效btnSave\u单击(对象发送方,事件参数e)
{
txtAddress.Text=“”;
字符串xmlPath=MapPath(“Books.xml”);
XmlDocument doc=新的XmlDocument();
//检查文件是否已存在
if(System.IO.File.Exists(xmlPath))
{
文档加载(xmlPath);
XmlNode bookNode=CreateBookNode(doc);
//获取对book节点的引用并将book节点附加到其中
XmlNode bookStoreNode=doc.SelectSingleNode(“书店”);
bookStoreNode.AppendChild(bookNode);
lblResult.Text=“XML文档已成功更新”;
txtAddress.Text=“”;响应。重定向(“http://www.google.com");
}
其他的
{
XmlNode declarationNode=doc.CreateXmlDeclaration(“1.0”、“1.0”、“1.0”);
doc.AppendChild(declarationNode);
XmlNode comment=doc.CreateComment(“该文件表示书店库存数据库的一个片段”);
文件附录(注释);
XmlNode bookstoreNode=doc.CreateElement(“bookstore”);
XmlNode bookNode=CreateBookNode(doc);
//将book节点附加到bookstore节点
bookstoreNode.AppendChild(bookNode);
//将书店节点附加到文档中
doc.AppendChild(书店节点);
lblResult.Text=“已成功创建XML文档”;
txtAddress.Text=“”;响应。重定向(“http://www.google.com");
}
保存文档(xmlPath);
}
XmlNode CreateBookNode(XmlDocument文档)
{
/*
XmlNode bookNode=doc.CreateElement(“book”);
//将“流派”属性添加到“书本”节点
xmldattribute genreAttribute=doc.CreateAttribute(“流派”);
genreAttribute.Value=txtgree.Text;
bookNode.Attributes.Append(genreAttribute);
http://www.java2s.com/Code/ASP/XML/SaveformdatatoXMLfile.htm
*/
XmlNode bookNode=doc.CreateElement(“book”);
//主节点声明(详情)
XmlNode SpecialsNode=doc.CreateElement(“详情”);
//主节点中的子节点声明(详细信息)
XmlNode nameNode=doc.CreateElement(“名称”);
XmlNode phoneNode=doc.CreateElement(“电话”);
XmlNode emailNode=doc.CreateElement(“电子邮件”);
XmlNode AddressNode=doc.CreateElement(“地址”);
//从htmlform获取textvalue
nameNode.InnerText=txtName.Text;
phoneNode.InnerText=txtPhone.Text;
emailNode.InnerText=txtEmail.Text;
AddressNode.InnerText=txtAddress.Text;
//在这里更新XML文件SpecialsNode有不同的子节点,它们正在被更新
specialsNode.AppendChild(nameNode);
AppendChild(phoneNode);
AppendChild(emailNode);
AppendChild(AddressNode);
bookNode.AppendChild(specialsNode);
//主节点声明(簿记详情)
XmlNode bookSpecialsNode=doc.CreateElement(“bookDetails”);
//主节点中的子节点声明(BookDetails)
XmlNode schoolNode=doc.CreateElement(“学校”);
XmlNode currentlevelNode=doc.CreateElement(“CurrentLevel”);
XmlNode GABDNode=doc.CreateElement(“GiveAwayBookDetails”);
XmlNode LRNode=doc.CreateElement(“LevelRequired”);
//从htmlform获取textvalue
schoolNode.InnerText=txtSchool.Text;
currentlevelNode.InnerText=txtCurrentLevel.Text;
GABDNode.InnerText=txtGABD.Text;
LRNode.InnerText=txtLR.Text;
//在这里更新XML文件SpecialsNode有不同的子节点,它们正在被更新
specialsNode.AppendChild(schoolNode);
AppendChild(currentlevelNode);
特殊节点。附加子节点(GABDNode);
specialsNode.AppendChild(LRNode);
bookNode.AppendChild(bookSpecialsNode);
还书节点;
}
公共静态字符串NewLineToBreak(字符串输入)
{
正则表达式正则表达式=新正则表达式(@“[\n|\r]+”;
返回regEx.Replace(输入“
”);
}
受保护的void txtAddress_加载(对象发送方,事件参数e)
{
txtAddress.Text=“Woodlands Drive 14\n Blk”;
}
您是否尝试过为要加载的资源使用完全限定路径
e、 g:
-或-
string xmlPath=MapPath(“~//Books.xml”);
另外,生产服务器是否引发异常?如果是这样,只在本地显示它们并使用本地http会话查看它们可能会有所帮助
customerrors的示例web.config节:
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
尝试给出完全限定路径,并查看它是否正在拾取XML文件。我确信可能是某些路径问题导致了异常。另外,请确保打开“自定义错误”模式,以便查看错误。尝试使用中所述的服务器.MapPath
,或者尝试使用页面。ResolveUrl
您在哪里验证字段,是否在客户端执行此操作?@user788312否也在服务器端执行此操作?因此您正在验证服务器端?if(System.IO.File.Exists(xmlPath))
是否属于else条件?我尝试了您建议的方法,但仍然没有用,实际上我已经尝试了使用“~/Books.xml”的方法,效果很好。当我在c代码中引入重定向url(如果有更好的方法)时,问题就开始了
string xmlPath = MapPath("~/<some_sub_dir>/Books.xml");
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>