asp.net-mvc – MVC 3 Url Helper提供不正确的URL
|
我正在为企业内部网站点开发MVC 3应用程序,并且我在URL助手方面遇到一些问题,有时候没有生成正确的URL.应用程序通过由我们的IT部门控制的访问管理器应用程序访问,该应用程序基本上提供标准化URL,以便用户不需要知道有关服务器的任何信息.例如,要直接在服务器上访问应用程序,我会访问: http://philsserver/App 通过访问管理器,我将使用IT部门提供的URL: http://secureintranet/PHILSAPP/App/ 我在我的应用程序的几个地方使用MVC URL帮助器 – 问题是有时“PHILSAPP”部分被遗漏 – 当我在“< a>”中使用它时链接,它工作,但当我在其他地方使用它,它没有. 例如,代码: <a href="@Url.Action("Index","FormsAndTemplates")">
正确创建链接为: < a href =“/ PHILSAPP / App / FormsAndTemplates”>. 以下代码: @Html.TextBox("lastName",ViewBag.LastName as string,new { @class = "input-mini",@autocomplete = Url.Action("QuickSearch","Employee") })
生产: <input autocomplete="/App/Employee/QuickSearch" class="input-mini" id="lastName" name="lastName" type="text" value="" /> 请注意,此URL不包含“PHILSAPP”部分.如果我在javascript中使用URL帮助程序或者除了“< a>”以外的任何地方,也会发生这种情况.标签.有谁知道为什么会发生这种情况?据我所知,对Url.Action的两次调用几乎都是一样的,所以我无法弄清楚为什么会发生这种情况.如果这个问题已经得到解答,我很抱歉,但我无法找到有关类似问题的人的任何信息.在此先感谢您的任何帮助. 更新: routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",// Route name
"{controller}/{action}/{id}",// URL with parameters
new { controller = "Home",action = "Index",id = UrlParameter.Optional });
更新2:正在使用的访问管理器是Tivoli Identity Manager,如果它给任何人提供任何线索. 解决方法正如nemesv上面指出的那样,答案是Url.Action方法始终生成URL为/ App / …但是访问管理器应用程序会识别某些标记(例如< a href =“/ App / .. .“>,< form action =”/ App /...“>等等)并将/ PHILSAPP添加到开头.我正在探索的解决方案是为UrlHelper和HtmlHelper编写一些扩展方法来生成绝对URL而不是相对URL,其中包含/ PHILSAPP的主机名将在web.config文件中指定.如果有人还有任何其他建议来解决这个问题,我会很高兴听到他们,但除此之外,我很满意使用它作为解决方法.一些样板代码开头: namespace MvcApplicationNameSpace
{
/// <summary>
/// Extension methods to the UrlHelper class for generating absolute URLs using
/// Web.config settings
/// </summary>
public static class UrlHelperExtensions
{
private static string BaseUrl
{
get
{
return System.Configuration.ConfigurationManager.AppSettings["BaseUrl"];
}
}
/// <summary>
/// Generates a string for the absolute URL to an action method
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url)
{
return BaseUrl + url.Action();
}
/// <summary>
/// Generates a string for the absolute URL to an action method with the
/// specified name
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url,string actionName)
{
return BaseUrl + url.Action(actionName);
}
/// <summary>
/// Generates a string for the absolute URL to an action method with the
/// specified name and route values
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="routeValues"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url,string actionName,object routeValues)
{
return BaseUrl + url.Action(actionName,routeValues);
}
/// <summary>
/// Generates a string for the absolute URL to an action method with the
/// specified name and route values
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="routeValues"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url,RouteValueDictionary routeValues)
{
return BaseUrl + url.Action(actionName,routeValues);
}
/// <summary>
/// Generates a string for the absolute URL to an action method and
/// controller
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="controllerName"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url,string controllerName)
{
return BaseUrl + url.Action(actionName,controllerName);
}
/// <summary>
/// Generates a string for the absolute URL to an action method and
/// controller,including route values
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="controllerName"></param>
/// <param name="routeValues"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url,string controllerName,controllerName,routeValues);
}
/// <summary>
/// Generates a string for the absolute URL to an action method and
/// controller,routeValues);
}
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – ELMAH日志的独立查看器应用程序
- asp.net – 将自定义类添加到HttpContext
- asp.net-mvc – 模型支持DB上下文已更改;考虑代码优先迁移
- ASP.NET MVC 3布局所有子视图中的ViewBag数据
- asp.net-mvc-3 – MVC 3 – 其他视图数据未出现在编辑器中用
- asp.net – 如何让用户登录2周?
- asp.net-mvc-2 – 引用用TextBoxFor()创建的控件ID
- ASP.NET主页面和内容页面中的表单元素
- ASP.NET – Response.Redirect不填充URL引用
- log4Net EventlogAppender不适用于Asp.Net 2.0 WebSite?
- asp.net-mvc – 用于OpenID OAuth简单身份验证的
- asp.net-mvc – ExceptionContext.ExceptionHand
- asp.net-mvc – ASP.NET MVC检查Controller或Act
- asp.net – VS 2010失败调试:HttpException在Lo
- asp.net – HTTPS停止使用IIS express
- .NET中的共享配置文件
- Asp.Net MVC 3 JSON模型绑定无法正常工作
- asp.net-mvc – ASP.NET MVC:很多路由 – 总是只
- asp.net – 在MVC5中使用java脚本代码 – 放在哪
- macos – ASP.NET CORE 2.1预览“Csc”任务不支持
