繁体   English   中英

MVC,控制器动作

[英]MVC, action for controller

我是第一次构建MVC应用。 当前,我的应用程序展示了一个小表格,该表格可让用户提供输入字符串(网址),并且在提交时将使用用户的输入在db表中创建新记录,并输出干净的网址。 我想在我的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;


    }
  }
 }

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

希望能有所帮助,

您需要的是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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM