วันศุกร์ที่ 5 ตุลาคม พ.ศ. 2555

Generate url string in Codebehind in MVC3

Yes I try "UrlHelper.GenerateUrl" before and it suck.
so came in to this solution
I use with static  Code so it came up here with reference to

using System.Web.Mvc;
using System.Web.Routing;

and declare this


HttpContextWrapper httpContextWrapper = new HttpContextWrapper( System.Web.HttpContext.Current );
UrlHelper urlHelper = new UrlHelper( new RequestContext( httpContextWrapper, RouteTable.Routes.GetRouteData( httpContextWrapper ) ) );

and use like this


string url = urlHelper.Action( "Action", "Controller name", new { id = 1 } );
sb.Append( String.Format( "{1}", url, "Link Text" ) );

more information can see solution in
http://stackoverflow.com/questions/2031995/call-urlhelper-in-models-in-asp-net-mvc



ไม่มีความคิดเห็น: