ASP.NET MVC ÊÓͼ¸ÅÀÀ (C#)
´Ë½Ì³ÌÏòÄãÌṩ ASP.NET MVC ÊÓͼ¼ò½é, ÊÓͼÊý¾Ý, ºÍ HTML Helpers¡£½Ì³Ì×îºó£¬ÄãÓ¦¸Ã»áÀí½âÈçºÎ´´½¨ÐÂÊÓͼ£¬´Ó¿ØÖÆÆ÷´«µÝÊý¾Ýµ½ÊÓͼ£¬ºÍʹÓà use HTML Helpers ÔÚÊÓͼÖÐÉú³ÉÄÚÈÝ¡£
Àí½âÊÓͼ
²»Ïñ ASP.NET »òÕßASP, ASP.NET MVC ²¢²»°üÀ¨ÈκÎÖ±½Ó¶ÔÓ¦Ò³ÃæµÄ¶«Î÷¡£ ÔÚ ASP.NET MVC Ó¦ÓóÌÐòÖУ¬Ó²ÅÌÉϲ¢²»´æÔÚÓëÄãÊäÈëä¯ÀÀÆ÷µØÖ·À¸ÖеÄURL¶ÔÓ¦µÄÒ³Ãæ¡£ÔÚASP.NET MVCÓ¦ÓóÌÐòÖÐÓëÒ³Ãæ×î½Ó½üµÄ¶«Î÷ÊdzÆΪÊÓͼµÄ¶«Î÷¡£
ASP.NET MVC Ó¦ÓóÌÐò, ´«ÈëµÄä¯ÀÀÆ÷ÇëÇó±»Ó³Éäµ½¿ØÖÆÆ÷ actions¡£¿ØÖÆÆ÷ action ¿ÉÄÜ·µ»ØÒ»¸öÊÓͼ¡£È»¶ø£¬¿ØÖÆÆ÷¿ÉÄÜÖ´ÐÐÆäËûÀàÐ͵Äaction£¬±ÈÈ罫ÄãÖض¨Ïòµ½ÁíÒ»¸ö¿ØÖÆÆ÷ action¡£
´úÂë1 °üº¬Ò»¸ö¼òµ¥µÄÃûΪ HomeControllerµÄ¿ØÖÆÆ÷¡£ HomeController ±©Â¶ÁË2¸ö¿ØÖÆÆ÷ actions ÃûΪ Index() ºÍ Details()¡£
´úÂë1 – HomeController.cs
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Details()
{
return RedirectToAction("Index");
}
}
}
Äã¿ÉÒÔÔÚä¯ÀÀÆ÷µØÖ·À¸ÖÐÊäÈëÒÔÏÂURLÀ´µ÷ÓõÚÒ»¸öaction, Index() action£º
/Home/Index
Äã¿ÉÒÔÔÚä¯ÀÀÆ÷µØÖ·À¸ÊäÈëÕâ¸öÀ´µ÷Óõڶþ¸öaction, Details() action£º
/Home/Details
Index() action ·µ»ØÒ»¸öÊÓͼ¡£´ó¶àÊýÄã´´½¨µÄaction¶¼ÊÇ·µ»ØÊÓͼµÄ¡£È»¶ø£¬Ò»¸öaction¿ÉÒÔ·µ»ØÆäËûÀàÐ굀 action result¡£ÀýÈ磬 Details() action ·µ»ØÒ»¸ö RedirectToActionResult £¬Ëü½«´«ÈëµÄÇëÇóÖض¨Ïòµ½ Index() action¡£
Index() action Ö»Óмòµ¥µÄÒ»ÐдúÂ룺
View();
ÕâÐдúÂë·µ»ØÒ»¸ö±ØÐëλÓÚÄãµÄweb·þÎñÆ÷ÒÔÏ·¾¶µÄÊÓͼ£º
\Views\Home\Index.aspx
¸Ã·¾¶µÄÊÓͼÓÉ¿ØÖÆÆ÷Ãû³ÆºÍ¿ØÖÆÆ÷actionÃû³ÆÀ´Íƶϡ£
Èç¹ûÄãÔ¸Ò⣬Äã¿ÉÒÔÏÔʽָ¶¨ÊÓͼÃû³Æ¡£ÏÂÃæÕâÐдúÂë·µ»ØÒ»¸öÃûΪ"Fred"µÄÊÓͼ£º
View(“Fred”);
µ±ÕâÐдúÂë±»Ö´ÐÐʱ£¬ÊÓͼ´ÓÒÔÏ·¾¶·µ»Ø£º
\Views\Home\Fred.aspx
Èç¹ûÄãҪΪÄãµÄASP.NET MVC³ÌÐò´´½¨µ¥Ôª²âÊÔÄÇôÏÔʽָ¶¨ÊÓͼÃû³ÆÊÇÒ»¸öºÃÖ÷Òâ¡£ÄÇÑù£¬Äã¿ÉÒÔ´´½¨µ¥Ôª²âÊÔÀ´ÑéÖ¤Ô¤ÆÚµÄÊÓͼÊÇ·ñÓÉÒ»¸ö¿ØÖÆÆ÷action·µ»Ø¡£
ÏòÊÓͼÌí¼ÓÄÚÈÝ
ÊÓͼÊÇÒ»¸ö¿ÉÒÔ°üº¬½Å±¾µÄ±ê×¼µÄ
Ïà¹ØÎĵµ£º
µÚÒ»£º
private void Button1_Click( object sender, System.EventArgs e )
{
Response.Redirect( Request.Url.ToString( ) );
}
µÚ¶þ£º
private void Button2_Click( object sender, System.EventArgs e )
{ ......
×î½ü£¬Òª°ÑÒ»¸öC/S½á¹¹µÄ³ÌÐò¸Ä³ÉB/S½á¹¹¡£ÆäÖоÍÓöµ½ÁË´òÓ¡µÄÎÊÌâ¡£Ñо¿ÁËһϣ¬webÏ´òÓ¡·½·¨´ó¸ÅÓÐÕâô¼¸ÖÖ£º
1¡¢ÀûÓÃIEÖ±½Ó´òÓ¡£¬ÕâÖÖ·½Ê½×î·½±ã£¬µ«ÊÇ¿ØÖÆÄÜÁ¦½Ï²î¡£
2¡¢ÀûÓÃACTIVE¿Ø¼þ£¬¿ÉÒÔ×Ô¼º×öÒ»¸öIEµÄ¿Ø¼þÀ´¿ØÖÆ´òÓ¡¡£
3¡¢Ë®¾§±¨±íµÈµÚÈý·½¹¤¾ß£¬¹ºÂò¼Û¸ñ°º¹ó¡£
¸ù¾ÝÎÒµÄÐèÇóÇé¿ö£¬ÓÃIEÖ ......
ʲôÊÇMVCģʽ
MVC£¨Model-View-Controller£¬Ä£ÐÍ—ÊÓͼ—¿ØÖÆÆ÷ģʽ£©ÓÃÓÚ±íʾһÖÖÈí¼þ¼Ü¹¹Ä£Ê½¡£Ëü°ÑÈí¼þϵͳ·ÖΪÈý¸ö»ù±¾²¿·Ö£ºÄ£ÐÍ£¨Model£©£¬ÊÓͼ£¨View£©ºÍ¿ØÖÆÆ÷£¨Controller£©¡£
ÄÇôMVCģʽºÍÎÒÃÇÊìϤµÄWebFormģʽÓÐʲô²»Í¬ÄØ£¿ËûµÄ¸÷¸ö²¿·ÖÓÖÊÇÔõÑù·Ö¹¤µÄÄØ£¿
ÎÒÃÇÏÈÀ´¿´Ò»ÏÂÆÕͨµÄWebFormģʽÏ£ ......
×î½ü×öµÄϵͳÖÐÀϳöÏÖµÄһЩÎÊÌⲻ̫Ã÷°×£¬ÔÚʹÓà Response.End¡¢Response.Redirect »ò Server.Transfer ʱ³öÏÖ ThreadAbortException £¬±¾À´ÏµÍ³ÊÇûÓÐÎÊÌâµÄ£¬ÔÚ±£´æÊý¾ÝʱҲ¿ÉÒÔÕý³££¬±¾À´Ê¹ÓÃtry-catch Óï¾äÊÇÓÃÀ´²¶»ñÒ»Òì³£Çé¿öµÄ£¬µ«ÏµÍ³Õý³££¬Àϲ¶»ñµ½ÏÂÃæµÄ¶«Î÷
##[²Ù×÷¼Ç¼]£º2010-02-23 9:25:12 Sys ......
Asp.NetÖгÉÔ±¡¢½ÇÉ«ÅäÖõľßÌå²½Ö裺
1£©×öºÃÍøÕ¾¹æ»®£¬·ÃÎÊȨÏÞÏàͬµÄÒ³Ãæ·Åµ½Í¬Ò»¸öÎļþ¼ÐÖУ»
2£©ÓÃC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe½«³ÉÔ±×ʸñÓë½ÇÉ«¹ÜÀíµÄÊý¾Ý¿â²¢ÈëÍøÕ¾Êý¾Ý¿â
3£©ÐÞ¸ÄÍøÕ¾µÄÅäÖÃÎļþweb.config£¬Ê¾ÀýÈçÏ£º
<?xml version="1.0"?>
<configura ......