예제 #1
0
        // GET: RECOM_BOOK_USER/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            RECOM_BOOK_USER rECOM_BOOK_USER = db.RECOM_BOOK_USER.Find(id);

            if (rECOM_BOOK_USER == null)
            {
                return(HttpNotFound());
            }

            return(RedirectToRoute(new { controller = "INTERNALBOOKS", action = "Details", id }));
        }
예제 #2
0
        public ActionResult Create([Bind(Include = "ISBN")] RECOM_BOOK_USER rECOM_BOOK_USER)
        {
            string isbn = Request["ISBN"];

            if (isbn.Length != 13)
            {
                ModelState.AddModelError("ISBN", "ISBN必须为13位数字编号");
                return(View());
            }
            var query = db.BOOKS.SqlQuery("SELECT * FROM BOOKS WHERE ISBN ='" + isbn + "'");
            int count = query.ToList().Count;

            if (query.ToList().Count > 0)
            {
                ModelState.AddModelError("ISBN", "该书已存在书库中");
                return(View());
            }

            HttpWebRequest  myRequest      = null;
            HttpWebResponse myHttpResponse = null;
            string          doubanurl      = "http://api.douban.com/book/subject/isbn/";
            string          geturl         = doubanurl + isbn;

            myRequest = (HttpWebRequest)WebRequest.Create(geturl);
            //myRequest.Method = "HEAD";
            //myRequest.AllowAutoRedirect = false;
            try
            {
                myHttpResponse = (HttpWebResponse)myRequest.GetResponse();
            }
            catch (WebException ex)
            {
                myHttpResponse = (HttpWebResponse)ex.Response;
            }


            StreamReader reader    = new StreamReader(myHttpResponse.GetResponseStream());
            string       xmldetail = reader.ReadToEnd();

            reader.Close();
            myHttpResponse.Close();
            myRequest.Abort();

            if (myHttpResponse != null)
            {
                if (myHttpResponse.StatusCode == HttpStatusCode.NotFound)
                {
                    ModelState.AddModelError("ISBN", "无效的ISBN");
                    return(View());
                }
            }

            var         books = new bookinfo();
            XmlDocument xml   = new XmlDocument();

            xml.LoadXml(xmldetail);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);

            nsmgr.AddNamespace("db", "http://www.w3.org/2005/Atom");
            XmlElement  root  = xml.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("/db:entry", nsmgr);

            foreach (XmlNode nodeData in nodes)
            {
                foreach (XmlNode childnode in nodeData.ChildNodes)
                {
                    string str = childnode.Name;
                    switch (str)
                    {
                    case "title":
                        books.Name = childnode.InnerText;
                        break;

                    case "link":
                        if (childnode.Attributes[1].Value == "image")
                        {
                            books.Imageurl = childnode.Attributes[0].Value;
                        }
                        break;

                    case "summary":
                        books.Summary = childnode.InnerText;
                        break;

                    case "db:attribute":
                    {
                        switch (childnode.Attributes[0].Value)
                        {
                        case "isbn13":
                            books.Isbn = childnode.InnerText;
                            break;

                        case "pages":
                            books.Pages = childnode.InnerText;
                            break;

                        case "author":
                            books.Author = childnode.InnerText;
                            break;

                        case "price":
                            books.Price = childnode.InnerText;
                            break;

                        case "publisher":
                            books.Publisher = childnode.InnerText;
                            break;

                        case "pubdate":
                            books.Pubdate = childnode.InnerText;
                            break;
                        }        //end switch
                        break;
                    }
                    } //end switch
                }     //end foreach
            }         //end foreach

            iSBN     = books.Isbn;
            bOOKNAME = books.Name;

            return(RedirectToRoute(new { controller = "RECOM_BOOK_USER", action = "AddBook", id = isbn }));
        }