2011-10-03 98 views
0

我有以下代码,可以在使用IIS7的本地主机上正常工作,但是当我将其上传到我的服务器时,它的行为与本地主机上的行为不一样 例如点击时应该检查验证它确实在我的本地,然后提交按钮重定向到URL如果验证是正确的Asp.net代码不能在本地主机上的实际服务器上执行

但是当同样是实际的服务器上看过来 The not working link

试过的问题开始当我介绍了一个重定向网址(在C#代码是Response.Redirect(“http://www.google.com”);)在C#代码...如果有一个更好的方式来做到这一切麻烦这不会是necessary..tks

这是我用

<%@ 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> 
+0

你在哪里验证字段,你在客户端做什么呢? – user788312

+0

@ user788312没有在服务器端以及 – JackyBoi

+0

所以你正在验证服务器端? if(System.IO.File.Exists(xmlPath))是否落入else条件? –

回答

0

您是否尝试过使用你试图加载资源完全合格的路径的代码?

例如为:

string xmlPath = MapPath("~/Books.xml"); 

- 或 -

string xmlPath = MapPath("~/<some_sub_dir>/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> 
+0

我尝试了你的建议仍然没有用,实际上我已经试过了这个与“〜/ Books.xml”,它工作正常的问题开始时,我介绍了一个重定向URL到C#代码,如果有更好的方法做到这一切这种麻烦不会是必要的。英文版 – JackyBoi

+0

您可以尝试从您的代码中评论Response.Redirect语句。 – user788312

+1

我注意到books.xml文件并没有在第一次创建的关于它以前它在我的ie缓存中 – JackyBoi

0

先给完全合格的路径,看看它的采摘XML文件。我相信这可能是导致异常的一些路径问题。另外请确保您打开自定义错误模式,以便您可以看到错误。

+0

我试过,也没有用 – JackyBoi

+0

您创建一个整数变量计数与初始值为0,如果在您的代码中发现错误,每次增加1并最终检查计数是否为0,如果它是调用您的重定向方法,否则将错误显示为标签或您想要的任何方式。 – user788312

相关问题