加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVC 3 Url Helper提供不正确的URL

发布时间:2020-12-16 00:00:16 所属栏目:asp.Net 来源:网络整理
导读:我正在为企业内部网站点开发MVC 3应用程序,并且我在URL助手方面遇到一些问题,有时候没有生成正确的URL.应用程序通过由我们的IT部门控制的访问管理器应用程序访问,该应用程序基本上提供标准化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);
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读