主攻ASP.NET.3.5.MVC架构之重生:Controller(七)
<%:RouteData.Values["controller"].ToString()%>
//通过View的RouteData.Values对象取得当前所有路由值,并动态加载路由值中的Controller的名称
//Routing
//客户端请求->URL Routing ->Route->Route Handler->Http Handler
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//定义不需要通过Routing处理的网址
//IgnoreRoute辅助方法是ASP.NET MVC(System.Web.Mvc)
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Test", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
//MapRoute辅助方法是ASP.NET MVC(System.Web.Mvc)
<%:RouteData.Values["controller"].ToString()%>
<%:RouteTable.Routes.GetVirtualPath(Request.RequestContext,new RouteValueDictionary(new {page=1})).VirtualPath %>
view:/?page=1
http://localhost:6782/Test/?page=1
//HandleUnknownAction 方法默认会响应"HTTP 404找不到资源"的错误信息
protected virtual void HandleUnknownAction(string actionName)
{
Response.Redirect("/");
}
//[ActionName("Default")]动作选取器 只允许Default Action
///[NonAction]不对外公开属性
//把action public 改成private也可以达到同样效果 代码如下
------------------------------------------------------
//[NonAction]
private ActionResult Index()
{
return View();
}
//跳转到另一个页面
------------------------------------------------------
//[NonAction]
[HttpPost]
public ActionResult Index()
{
return new RedirectResult("/");
}
//controller辅助方法,跳转到另一个页面 ,此方法多用
------------------------------------------------------
[HttpPost]
public ActionResult Index()
{
return Redirect("/");
}
//指定index.apsx页面显示 Redirect不会跳转
------------------------------------------------------
[HttpPost]
public ActionResult Index()
{
return Redirect("/");
}
public ActionResult About()
{
return View("Index");
}
//EmptyResult不会执行任何响应客户端的程序,所以也不返回任何数据,不需要视图
------------------------------------------------------
public ActionResult Empty()
{
return new EmptyResult();
}
//EmptyResult改写,这样简单
--------------------------------------------------
public void Empty()
{
return;
}
//EmptyResult不会执行任何响应客户端的程序,所以也不返回任何数据,不需要视图
------------------------------------------------------
public ActionResult Empty()
{
return new EmptyResult();
}
//301——删除请求数据
//302——在其他地址发现了请求数据
//Response.Redirect()方法 HTTP302暂时转向
------------------------------------------------------
public void Redirect()
{
Response.Redirect("/vipuser/login");
}
//Asp.net 4.0新增Response.RedirectPermanent()建立HTTP301永久转向
------------------------------------------------------
public void Redirect()
{
Response.RedirectPermanent("/vipuser/login");
}
//响应xml content
------------------------------------------------------
public ActionResult Content()
{
return Content("<Root><Text>context</Text></Root>", "text/xml", Encoding.UTF8);
}
//响应html content
------------------------------------------------------
public ActionResult Content()
{
string strHTML = " <h2>Test</h2>";
return Content(strHTML);
}
//响应html content
------------------------------------------------------
public string Content()
{
string strHTML = " <h2>Test</h2>";
return strHTML;
}
//输出文件内容 content
------------------------------------------------------
public ActionResult GetFile()
{
return File(Server.MapPath("~/Content/Admin/images/background.png"), "image/png");
}