本文整理汇总了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());
}
示例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));
}
示例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);
}
示例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\">→</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\">→</span></a>",
urlHelper.Action(actionName, new
{
SortOrder = queryOptions.SortOrder,
SortField = queryOptions.SortField,
CurrentPage = queryOptions.CurrentPage + 1,
PageSize = queryOptions.PageSize
}));
}
}
示例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);
}
示例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)));
}
示例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));
}
示例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();
}
示例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);
}
示例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;
}
示例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);
}
示例12: SetUp
public void SetUp()
{
_serviceProxy = new Mock<ServiceProxy>();
var type = typeof (ServiceProxy);
type.SetFieldValue("_instance", _serviceProxy.Object);
_urlHelper = new UrlHelper();
}
示例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));
}
}
}
示例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));
}
示例15: BuildPreviousLink
private static string BuildPreviousLink(UrlHelper urlHelper, QueryOptions queryOptions, string actionName)
{
return string.Format(
"<a href=\"{0}\"><span aria-hidden=\"true\">←</span> Previous</a>",
urlHelper.Action(actionName, new
{
SortOrder = queryOptions.SortOrder,
SortField = queryOptions.SortField,
CurrentPage = queryOptions.CurrentPage - 1,
PageSize = queryOptions.PageSize
}));
}