MVC,控制器动作

时间:2009-08-17 15:49:39

标签: c# asp.net-mvc

我是第一次构建MVC应用程序。目前,我的应用程序提供了一个小表单,它将让用户提供输入字符串(一个url)并在提交时,将使用用户的输入在db表中创建一个新记录,并输出一个干净的url。我想在我的homecontroller文件中添加一个条件:

1)检查数据库表中是否存在“url”输入 2)如果是这样,将显示该记录经文创建重复记录。

    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;


    }
  }
 }

2 个答案:

答案 0 :(得分:0)

您需要将[AcceptVerbs(HttpVerbs.Post)]添加到您希望接受表单帖子的Create方法。

希望有所帮助,

答案 1 :(得分:0)

您需要的是 ShortUrlFunctions 类上的一种方法,可以检查数据库中是否存在给定的 url 。如果该方法名为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;
        }
    }
}