2009-08-17 136 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