MVC 3 Url Helper Giving Incorrect URL

只愿长相守 提交于 2019-12-05 23:15:14
Phil

As nemesv pointed out above, the answer was that the Url.Action method was always generating the URL as /App/... but the access manager application would recognize certain tags (such as <a href="/App/...">, <form action="/App/...">, etc.) and would add the /PHILSAPP to the beginning. The solution I am exploring is to write some extension methods for the UrlHelper and HtmlHelper to generate absolute URLs rather than relative URLs where the host name including the /PHILSAPP will be specified in the web.config file. If anyone still has any other suggestions to solve this I would be happy to hear them but otherwise I am satisfied with using this as a work-around.

Some boilerplate code to start with:

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, string actionName, 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 actionName, 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 actionName, string controllerName, object routeValues)
        {
            return BaseUrl + url.Action(actionName, controllerName, routeValues);
        }

        /// <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 actionName, string controllerName, RouteValueDictionary routeValues)
        {
            return BaseUrl + url.Action(actionName, controllerName, routeValues);
        }
    }
}

We exactly had the same problem behind our secure entry server. For REST calls, we wanted to generate urls on the server side to make them available in java script. But they didn't include the sub-path added by the secure entry server.

So we came up with a workaround like that (rendered in layout page):

<a id="urlBase" href="/" style="display: none;"></a>
<script type="text/javascript">
    baseUrl = document.getElementById('urlBase').getAttribute('href');
</script>

href="/" has been substitued by the entry server by href="/path/", and we could easily concatenate baseUrl with our relative paths when accessing REST services.

Hope it helps in your case too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!