当前位置: 首页>>代码示例>>C#>>正文


C# UrlHelper类代码示例

本文整理汇总了C#中UrlHelper的典型用法代码示例。如果您正苦于以下问题:C# UrlHelper类的具体用法?C# UrlHelper怎么用?C# UrlHelper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


UrlHelper类属于命名空间,在下文中一共展示了UrlHelper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MenuItemCheveron

        public static MvcHtmlString MenuItemCheveron(this HtmlHelper htmlHelper,
                                                     string text, string action, string controller,
                                                     string cheveronText, string area = null)
        {
            var li = new TagBuilder("li");
            var routeData = htmlHelper.ViewContext.RouteData;

            var currentAction = routeData.GetRequiredString("action");
            var currentController = routeData.GetRequiredString("controller");
            var currentArea = routeData.DataTokens["area"] as string;
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);

            if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &&
                string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase) &&
                string.Equals(currentArea, area, StringComparison.OrdinalIgnoreCase))
            {
                li.AddCssClass("active");
            }
            var linkBuilder = new TagBuilder("a");
            linkBuilder.MergeAttribute("href", urlHelper.Action(action, controller));

            var htmlText = li.ToString(TagRenderMode.StartTag);
            htmlText += linkBuilder.ToString(TagRenderMode.StartTag);
            htmlText += text;
            htmlText += linkBuilder.ToString(TagRenderMode.EndTag);
            htmlText += li.ToString(TagRenderMode.EndTag);
            return MvcHtmlString.Create(htmlText.ToString());
        }
开发者ID:sg0193,项目名称:Waterforafrica,代码行数:28,代码来源:HtmlExtensions.cs

示例2: ImageLink

    public static MvcHtmlString ImageLink(this HtmlHelper html, string action, string controller, object routeValues, string imageURL, string hoverImageURL, string alternateText, object linkHtmlAttributes, object imageHtmlAttributes)
    {
        // Create an instance of UrlHelper
        UrlHelper url = new UrlHelper(html.ViewContext.RequestContext);
        // Create image tag builder
        TagBuilder imageBuilder = new TagBuilder("img");
        // Add image attributes
        imageBuilder.MergeAttribute("src", imageURL);
        imageBuilder.MergeAttribute("alt", alternateText);
        imageBuilder.MergeAttribute("title", alternateText);

        //support for hover
        imageBuilder.MergeAttribute("onmouseover", "this.src='" + hoverImageURL + "'");
        imageBuilder.MergeAttribute("onmouseout", "this.src='" + imageURL + "'");
        imageBuilder.MergeAttribute("border", "0");

        imageBuilder.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes));
        // Create link tag builder
        TagBuilder linkBuilder = new TagBuilder("a");
        // Add attributes
        linkBuilder.MergeAttribute("href", url.Action(action, controller, new RouteValueDictionary(routeValues)));
        linkBuilder.InnerHtml = imageBuilder.ToString(TagRenderMode.SelfClosing);
        linkBuilder.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes));
        // Render tag
        return MvcHtmlString.Create(linkBuilder.ToString(TagRenderMode.Normal));
    }
开发者ID:lgulliver,项目名称:ImageLinkHelper,代码行数:26,代码来源:ImageLinkHelper.cs

示例3: ActionImage

        public static MvcHtmlString ActionImage(this HtmlHelper html, string action, string controllerName, object routeValues, string imagePath, string alt, string confirmMessage, bool newWindow)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);

            // build the <img> tag
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", url.Content(imagePath));
            imgBuilder.MergeAttribute("alt", alt);
            string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");
            anchorBuilder.MergeAttribute("href", url.Action(action, controllerName, routeValues));
            anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside

            if (!string.IsNullOrEmpty(confirmMessage))
                anchorBuilder.MergeAttribute("onclick", "return confirm('" + confirmMessage + "')");

            if (newWindow)
                anchorBuilder.MergeAttribute("target", "_blank");

            string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(anchorHtml);
        }
开发者ID:anlugifa,项目名称:BootWrapper,代码行数:25,代码来源:MvcHtmlExtentions.cs

示例4: BuildNextLink

 private static string BuildNextLink(
     UrlHelper urlHelper, QueryOptions queryOptions, string actionName)
 {
     if (queryOptions.CurrentPage != queryOptions.TotalPages)
     {
         return string.Format(
             "<a href=\"{0}\">Next <span aria-hidden=\"true\">&rarr;</span></a>",
             urlHelper.Action(actionName, new
             {
                 SortOrder = queryOptions.SortOrder,
                 SortField = queryOptions.SortField,
                 CurrentPage = queryOptions.CurrentPage + 1,
                 PageSize = queryOptions.PageSize
             }));
     }
     else
     {
         return string.Format(
             "<a href=\"{0}\" class=\"disabled\">Next <span aria-hidden=\"true\">&rarr;</span></a>",
             urlHelper.Action(actionName, new
             {
                 SortOrder = queryOptions.SortOrder,
                 SortField = queryOptions.SortField,
                 CurrentPage = queryOptions.CurrentPage + 1,
                 PageSize = queryOptions.PageSize
             }));
     }
 }
开发者ID:loukajaj,项目名称:CSharpHomeLibrary,代码行数:28,代码来源:HtmlHelperExtension.cs

示例5: SetUp

		public void SetUp()
		{
			string siteRoot = GetSiteRoot();
			string viewPath = Path.Combine(siteRoot, "RenderingTests\\Views");
			Layout = null;
			PropertyBag = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
			Helpers = new HelperDictionary();
			StubMonoRailServices services = new StubMonoRailServices();
			services.UrlBuilder = new DefaultUrlBuilder(new StubServerUtility(), new StubRoutingEngine());
			services.UrlTokenizer = new DefaultUrlTokenizer();
			UrlInfo urlInfo = new UrlInfo(
				"example.org", "test", "/TestBrail", "http", 80,
				"http://test.example.org/test_area/test_controller/test_action.tdd",
				Area, ControllerName, Action, "tdd", "no.idea");
			StubEngineContext = new StubEngineContext(new StubRequest(), new StubResponse(), services,
													  urlInfo);
			StubEngineContext.AddService<IUrlBuilder>(services.UrlBuilder);
			StubEngineContext.AddService<IUrlTokenizer>(services.UrlTokenizer);
			StubEngineContext.AddService<IViewComponentFactory>(ViewComponentFactory);
			StubEngineContext.AddService<ILoggerFactory>(new ConsoleFactory());
			StubEngineContext.AddService<IViewSourceLoader>(new FileAssemblyViewSourceLoader(viewPath));
			

			ViewComponentFactory = new DefaultViewComponentFactory();
			ViewComponentFactory.Service(StubEngineContext);
			ViewComponentFactory.Initialize();

			ControllerContext = new ControllerContext();
			ControllerContext.Helpers = Helpers;
			ControllerContext.PropertyBag = PropertyBag;
			StubEngineContext.CurrentControllerContext = ControllerContext;


			Helpers["urlhelper"] = Helpers["url"] = new UrlHelper(StubEngineContext);
			Helpers["htmlhelper"] = Helpers["html"] = new HtmlHelper(StubEngineContext);
			Helpers["dicthelper"] = Helpers["dict"] = new DictHelper(StubEngineContext);
			Helpers["DateFormatHelper"] = Helpers["DateFormat"] = new DateFormatHelper(StubEngineContext);


			//FileAssemblyViewSourceLoader loader = new FileAssemblyViewSourceLoader(viewPath);
//			loader.AddAssemblySource(
//				new AssemblySourceInfo(Assembly.GetExecutingAssembly().FullName,
//									   "Castle.MonoRail.Views.Brail.Tests.ResourcedViews"));

			viewEngine = new AspViewEngine();
			viewEngine.Service(StubEngineContext);
			AspViewEngineOptions options = new AspViewEngineOptions();
			options.CompilerOptions.AutoRecompilation = true;
			options.CompilerOptions.KeepTemporarySourceFiles = false;
			ICompilationContext context = 
				new CompilationContext(
					new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory),
					new DirectoryInfo(siteRoot),
					new DirectoryInfo(Path.Combine(siteRoot, "RenderingTests\\Views")),
					new DirectoryInfo(siteRoot));

			List<ICompilationContext> compilationContexts = new List<ICompilationContext>();
			compilationContexts.Add(context);
			viewEngine.Initialize(compilationContexts, options);
		}
开发者ID:ralescano,项目名称:castle,代码行数:60,代码来源:IntegrationViewTestFixture.cs

示例6: Breadcrumb

        public Breadcrumb()
        {
            this.IsVisible = false;

            UrlHelper urlHelper = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
            this.BreadcrumbPaths = new List<KeyValuePair<string, string>>();
            this.BreadcrumbPaths.Add(new KeyValuePair<string, string>(GeneralTexts.Home, HomeUrlHelper.Home_Index(urlHelper)));
        }
开发者ID:jordivila,项目名称:Net_MVC_NLayer_Generator,代码行数:8,代码来源:BreadcrumbModel.cs

示例7: Favicon

        public static MvcHtmlString Favicon(this HtmlHelper helper)
        {
            UrlHelper uh = new UrlHelper(helper.ViewContext.RequestContext);

            string faviconUrl = uh.Content("~/Images/favicon.ico");

            return new MvcHtmlString(string.Format("<link href=\"{0}\" rel=\"shortcut icon\" type=\"image/x-icon\" />", faviconUrl));
        }
开发者ID:prabhakara,项目名称:Dexter-Blog-Engine,代码行数:8,代码来源:HtmlHelperHelper.cs

示例8: SitemapPingerService

 /// <summary>
 /// Initializes a new instance of the <see cref="SitemapPingerService"/> class.
 /// </summary>
 /// <param name="loggingService">The logging service.</param>
 /// <param name="urlHelper">The URL helper.</param>
 public SitemapPingerService(
     ILoggingService loggingService,
     UrlHelper urlHelper)
 {
     this.loggingService = loggingService;
     this.urlHelper = urlHelper;
     this.httpClient = new HttpClient();
 }
开发者ID:MarcusPozzan,项目名称:ASP.NET-MVC-Boilerplate,代码行数:13,代码来源:SitemapPingerService.cs

示例9: RequestContextProperty

        public void RequestContextProperty() {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            UrlHelper urlHelper = new UrlHelper(requestContext);

            // Assert
            Assert.AreEqual(requestContext, urlHelper.RequestContext);
        }
开发者ID:sanyaade-mobiledev,项目名称:ASP.NET-Mvc-2,代码行数:8,代码来源:UrlHelperTest.cs

示例10: SitemapService

 /// <summary>
 /// Initializes a new instance of the <see cref="SitemapService" /> class.
 /// </summary>
 /// <param name="cacheService">The cache service.</param>
 /// <param name="loggingService">The logging service.</param>
 /// <param name="urlHelper">The URL helper.</param>
 public SitemapService(
     ICacheService cacheService,
     ILoggingService loggingService,
     UrlHelper urlHelper)
 {
     this.cacheService = cacheService;
     this.loggingService = loggingService;
     this.urlHelper = urlHelper;
 }
开发者ID:vivekdhami,项目名称:ASP.NET-MVC-Boilerplate,代码行数:15,代码来源:SitemapService.cs

示例11: Setup

		public void Setup()
		{
			var controllerContext = new ControllerContext();
			var engineContext = new StubEngineContext { CurrentControllerContext = controllerContext, UrlInfo = new UrlInfo("", "home", "index", "", "") };

			helper = new UrlHelper(engineContext) { UrlBuilder = new DefaultUrlBuilder() };
			helper.UrlBuilder.UseExtensions = false;
			controllerContext.Helpers.Add(helper);
		}
开发者ID:rambo-returns,项目名称:MonoRail,代码行数:9,代码来源:UrlHelperTestCase.cs

示例12: SetUp

        public void SetUp()
        {
            _serviceProxy = new Mock<ServiceProxy>();

            var type = typeof (ServiceProxy);
            type.SetFieldValue("_instance", _serviceProxy.Object);

            _urlHelper = new UrlHelper();
        }
开发者ID:kiran-singh,项目名称:ReflectingForTesting,代码行数:9,代码来源:UrlHelperFixture.cs

示例13: HandleUnauthorizedRequest

        protected void HandleUnauthorizedRequest(ref AuthorizationContext filterContext)
        {
            UrlHelper urlHelper = null;
            if (filterContext.Controller is Controller)
            {
                urlHelper = ((Controller)filterContext.Controller).Url;
            }
            else
            {
                urlHelper = new UrlHelper(filterContext.RequestContext);
            }

            bool isAjaxRequest = filterContext.RequestContext.HttpContext.Request.IsAjaxRequest();

            Func<string, JsonResult> setResult = delegate(string msg)
            {
                DataResultBoolean response = new DataResultBoolean()
                {
                    IsValid = false,
                    Message = msg,
                    MessageType = DataResultMessageType.Error,
                    Data = false
                };
                JsonResult result = new JsonResult();
                result.Data = response;
                return result;
            };

            if (!this.CheckSessionExpired(filterContext.HttpContext))
            {
                if (isAjaxRequest)
                {
                    filterContext.Result = setResult($customNamespace$.Resources.General.GeneralTexts.SessionExpired);
                }
                else
                {
                    filterContext.Result = new RedirectResult(ErrorUrlHelper.SessionExpired(urlHelper));
                }
            }

            // break execution in case a result has been already set
            if (filterContext.Result != null)
                return;

            if (!this.CheckIsAutohrized(filterContext.HttpContext))
            {
                if (isAjaxRequest)
                {
                    filterContext.Result = setResult($customNamespace$.Resources.General.GeneralTexts.PermissionDenied);
                }
                else
                {
                    filterContext.Result = new RedirectResult(ErrorUrlHelper.UnAuthorized(urlHelper));
                }
            }
        }
开发者ID:jordivila,项目名称:Net_MVC_NLayer_Generator,代码行数:56,代码来源:AuthorizeAttibuteExtended.cs

示例14: ActionLinkImage

        public static MvcHtmlString ActionLinkImage(this HtmlHelper htmlHelper, string imageLocation, string altTag, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);

            var link = new TagBuilder("a");
            link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
            link.MergeAttribute("target", (string)htmlAttributes["target"]); htmlAttributes.Remove("target");
            link.InnerHtml = htmlHelper.Image(imageLocation, altTag, htmlAttributes).ToString();

            return MvcHtmlString.Create(link.ToString(TagRenderMode.Normal));
        }
开发者ID:wduffy,项目名称:Toltech.Mvc,代码行数:11,代码来源:ActionLinkImage.cs

示例15: BuildPreviousLink

 private static string BuildPreviousLink(UrlHelper urlHelper, QueryOptions queryOptions, string actionName)
 {
     return string.Format(
     "<a href=\"{0}\"><span aria-hidden=\"true\">&larr;</span> Previous</a>",
     urlHelper.Action(actionName, new
     {
         SortOrder = queryOptions.SortOrder,
         SortField = queryOptions.SortField,
         CurrentPage = queryOptions.CurrentPage - 1,
         PageSize = queryOptions.PageSize
     }));
 }
开发者ID:nikkiLuan,项目名称:Knockout-MVC,代码行数:12,代码来源:HtmlHelperExtensions.cs


注:本文中的UrlHelper类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。