2011-10-03 53 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

相關問題