我正在構建一個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;
}
}
}
你的問題到底是什麼?你有錯誤嗎? – 2009-08-17 15:53:05
模型在哪裏?這就是大量的條件測試(控制器中的過濾垃圾輸入是OK)應該是並且絕對應該提升條件的地方(控制器可以在ModelState中打包並返回)。 – 48klocs 2009-08-17 16:08:50