2009-08-17 133 views
0

我正在構建一個MVC應用程序的第一次。目前,我的應用程序提供了一個小表單,可以讓用戶提供輸入字符串(url),並在提交時使用用戶輸入在db表中創建新記錄,並輸出乾淨的url。我想在我的homecontroller文件中添加一個條件:MVC,控制器的動作

1)檢查數據庫表中是否存在「url」輸入,如果是,將顯示該條記錄創建重複記錄。

Index View -------------------- 

     <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> 

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

     <html xmlns="http://www.w3.org/1999/xhtml" > 
     <head runat="server"> 
      <title></title> 
     </head> 
     <body> 
      <div> 
      <form action="/Home/Create" method="post"> 
      Enter: <input type="text" name="urlToShorten" id="shortenUrlInput" /> 
      <input type="submit" value="Shorten" /> 
      </form> 

      </div> 



     </body> 
     </html> 

    Create View ------------------------------------------------------------ 

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

    <html xmlns="http://www.w3.org/1999/xhtml" > 
    <head runat="server"> 
     <title></title> 
    </head> 
    <body> 
     <div> 
     The clean url:<br /> 
     <%= string.Format("{0}/{1}",Request.Url.GetLeftPart(UriPartial.Authority),ViewData["shortUrl"]) %> 

     </div> 
    </body> 
    </html> 

    Homecontroller---------------------------------------------------------- 


     using System; 
     using System.Collections.Generic; 
     using System.Linq; 
     using System.Web; 
     using System.Web.Mvc; 
     using System.Web.Mvc.Ajax; 
     using ShortUrl.Models; 

     namespace ShortUrl.Controllers 
     { 
      [HandleError] 
      public class HomeController : Controller 
      { 
       public ActionResult Index() 
       { 
        return View(); 

       } 

       [HandleError] 
       public ActionResult Create(string urlToShorten) 
       { 
        if (string.IsNullOrEmpty(urlToShorten)) 

        { 

         return RedirectToAction("Index"); 
        } 



        else 
        { 
         long result = ShortUrlFunctions.InsertUrl(urlToShorten); 
         ViewData["shortUrl"] = result; 
         return View("Create"); 
        } 
       } 
       [HandleError] 
       public ActionResult Resolve(long? id) 
       { 
        if (!id.HasValue || id.Value == 0) 
        { 
         return RedirectToAction("Index"); 
        } 
        else 
        { 
         string url = ShortUrlFunctions.RetrieveUrl(id.Value); 
         if (url == null) 
         { 
          return RedirectToAction("Index"); 
         } 
         else 
         { 

          return Redirect(url); 
         } 
        } 
       } 
      } 
     } 

------------ShortUrlFunctions.cs 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace ShortUrl.Models 
{ 
    public static class ShortUrlFunctions 
    { 
     public static string RetrieveUrl(long inputKey) 
     { 
      using (ShortUrlEntities db = new ShortUrlEntities()) 
      { 


        var existingUrl = (from t in db.ShortURLSet where 
t.id == inputKey select t).Take(1); 
        if (existingUrl.Count() == 1) 
        { 
         return existingUrl.First().url; 

        } 
        else 
        { 
         return null; 
        } 
      } 
     } 

      public static long InsertUrl(string inputUrl) 
      { 
       long result = 0; 
       if(!string.IsNullOrEmpty(inputUrl)) 
       { 
        using (ShortUrlEntities db = new ShortUrlEntities()) 
        { 
         if (inputUrl.IndexOf(@"://") == -1) inputUrl = 
"http://" + inputUrl; 
          ShortURL su = new ShortURL(); 
         su.url = inputUrl; 
         db.AddToShortURLSet(su); 
         db.SaveChanges(); 
         result = su.id; 

      } 
     } 

       return result; 


    } 
    } 
} 
+1

你的問題到底是什麼?你有錯誤嗎? – 2009-08-17 15:53:05

+0

模型在哪裏?這就是大量的條件測試(控制器中的過濾垃圾輸入是OK)應該是並且絕對應該提升條件的地方(控制器可以在ModelState中打包並返回)。 – 48klocs 2009-08-17 16:08:50

回答

0

你需要的是你的ShortUrlFunctions的方法類,它可以檢查一個給定網址在數據庫中存在。如果該方法被命名爲GetIdForUrl那麼所有你需要做的是改變你的Create行動如下:

 [HandleError] 
     public ActionResult Create(string urlToShorten) 
     { 
      if (string.IsNullOrEmpty(urlToShorten)) 
      { 
       return RedirectToAction("Index"); 
      } 

      // No need for an else here since you have a return on the if above. 

      long result = ShortUrlFunctions.GetIdForUrl(urlToShorten); 


      // I am assuming that the function above returns 0 if url is not found.    
      if (result == 0) 
      { 
       result = ShortUrlFunctions.InsertUrl(urlToShorten); 
      } 

      ViewData["shortUrl"] = result; 
      return View("Create"); 
     } 

編輯:(針對您的評論)

GetIdForUrl示例實現將是:

public static long GetIdForUrl(string inputUrl) 
{ 
    using (ShortUrlEntities db = new ShortUrlEntities()) 
    { 
     var checkUrl = (from t in db.ShortURLSet 
         where t.url == inputUrl select t.id); 

     if (checkUrl.Count() == 1) 
     { 
      return checkUrl.First(); 
     } 
     else 
     { 
      return 0; 
     } 
    } 
} 
+0

感謝您的迴應。我的功能應該是這樣嗎? 公共靜態長GetIdForUrl(串inputUrl) { 使用(ShortUrlEntities分貝=新ShortUrlEntities()){ VAR = checkUrl(從db.ShortURLSet t其中t.url == inputUrl選擇噸)。取( 1); (checkUrl.Count()== 1) { return checkUrl.id; – 2009-08-17 20:25:06

+0

評論的結尾已被裁剪,但從我收集的內容中可以發現。但是請注意,查詢後不需要「.Take(1)」;你可以檢查「.Count()== 1」沒有「Take」。另外我看不到結尾,但我認爲如果找不到記錄,則返回0。 – paracycle 2009-08-17 21:13:43

+0

針對您的評論擴展了答案。 – paracycle 2009-08-17 21:19:56

0

您需要將[AcceptVerbs(HttpVerbs.Post)]添加到您希望接受表單發佈的Create方法中。

希望幫助,

+0

他不需要該屬性,因爲該屬性用於將處理限制爲僅限於POST。另一方面,是的,如果他只接受POST請求,那麼這個屬性會很好。 – paracycle 2009-08-17 16:05:42

+0

確實,好點,做得很好;) – 2009-08-17 17:24:00