当前位置:中国站长下载文章中心网页编程.NET编程 → Asp.net Mvc Framework可以在Controller中使用的Url.Action方法

Asp.net Mvc Framework可以在Controller中使用的Url.Action方法

减小字体 增大字体 作者:重典  来源:CHINAZ用户投稿  发布时间:2008-9-1 18:03:04

原本的Url.Action方法是利用RouteCollection来实现Url的Routing的。

所以这里用一个扩展方法重现一下

以下为引用的内容:

using System.Web.Routing;
    
static public class CUrl {
        
public static string Action(this Controller c, string controller, string action) {
            RouteValueDictionary rvd 
= new RouteValueDictionary();
            rvd.Add(
"controller", controller);
            rvd.Add(
"action", action);
            
return RouteTable.Routes.GetVirtualPath(c.ControllerContext, rvd).VirtualPath;
        }
    }

使用方法:

以下为引用的内容:

public ActionResult Index() {
            ViewData[
"Message"= this.Action("home""about");
            
return View();
        }