Asp.Net MVC:كيف يمكنني الحصول على عنوان url الظاهري لوحدة التحكم/العرض الحالي؟

StackOverflow https://stackoverflow.com/questions/151963

  •  02-07-2019
  •  | 
  •  

سؤال

هل من الممكن الحصول على المسار/عنوان URL الافتراضي المرتبط بإجراء وحدة التحكم أو بطريقة العرض؟لقد رأيت أن Preview 4 أضاف مساعد LinkBuilder.BuildUrlFromExpression، ولكنه ليس مفيدًا جدًا إذا كنت تريد استخدامه على الجهاز الرئيسي، نظرًا لأن نوع وحدة التحكم يمكن أن يكون مختلفًا.هي موضع تقدير أي أفكار.

هل كانت مفيدة؟

المحلول 3

يمكنك الحصول على تلك البيانات من ViewContext.RouteData.وفيما يلي بعض الأمثلة لكيفية الوصول إلى (واستخدام) تلك المعلومات:

/// تمت إضافتها إلى فئات viewmasterpage وviewpage وviewusercontrol الأساسية:

public bool IsController(string controller)
{
    if (ViewContext.RouteData.Values["controller"] != null)
    {
        return ViewContext.RouteData.Values["controller"].ToString().Equals(controller, StringComparison.OrdinalIgnoreCase);
    }
    return false;
}
public bool IsAction(string action)
{
    if (ViewContext.RouteData.Values["action"] != null)
    {
        return ViewContext.RouteData.Values["action"].ToString().Equals(action, StringComparison.OrdinalIgnoreCase);
    }
    return false;
}
public bool IsAction(string action, string controller)
{
    return IsController(controller) && IsAction(action);
}

/// بعض طرق الامتداد التي أضفتها إلى فئة UrlHelper.

public static class UrlHelperExtensions
{
    /// <summary>
    /// Determines if the current view equals the specified action
    /// </summary>
    /// <typeparam name="TController">The type of the controller.</typeparam>
    /// <param name="helper">Url Helper</param>
    /// <param name="action">The action to check.</param>
    /// <returns>
    ///     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsAction<TController>(this UrlHelper helper, LambdaExpression action) where TController : Controller
    {
        MethodCallExpression call = action.Body as MethodCallExpression;
        if (call == null)
        {
            throw new ArgumentException("Expression must be a method call", "action");
        }

        return (call.Method.Name.Equals(helper.ViewContext.ViewName, StringComparison.OrdinalIgnoreCase) &&
                typeof(TController) == helper.ViewContext.Controller.GetType());
    }

    /// <summary>
    /// Determines if the current view equals the specified action
    /// </summary>
    /// <param name="helper">Url Helper</param>
    /// <param name="actionName">Name of the action.</param>
    /// <returns>
    ///     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsAction(this UrlHelper helper, string actionName)
    {
        if (String.IsNullOrEmpty(actionName))
        {
            throw new ArgumentException("Please specify the name of the action", "actionName");
        }
        string controllerName = helper.ViewContext.RouteData.GetRequiredString("controller");
        return IsAction(helper, actionName, controllerName);
    }

    /// <summary>
    /// Determines if the current view equals the specified action
    /// </summary>
    /// <param name="helper">Url Helper</param>
    /// <param name="actionName">Name of the action.</param>
    /// <param name="controllerName">Name of the controller.</param>
    /// <returns>
    ///     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsAction(this UrlHelper helper, string actionName, string controllerName)
    {
        if (String.IsNullOrEmpty(actionName))
        {
            throw new ArgumentException("Please specify the name of the action", "actionName");
        }
        if (String.IsNullOrEmpty(controllerName))
        {
            throw new ArgumentException("Please specify the name of the controller", "controllerName");
        }

        if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
        {
            controllerName = controllerName + "Controller";
        }

        bool isOnView = helper.ViewContext.ViewName.SafeEquals(actionName, StringComparison.OrdinalIgnoreCase);
        return isOnView && helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase);
    }

    /// <summary>
    /// Determines if the current request is on the specified controller
    /// </summary>
    /// <param name="helper">The helper.</param>
    /// <param name="controllerName">Name of the controller.</param>
    /// <returns>
    ///     <c>true</c> if the current view is on the specified controller; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsController(this UrlHelper helper, string controllerName)
    {
        if (String.IsNullOrEmpty(controllerName))
        {
            throw new ArgumentException("Please specify the name of the controller", "controllerName");
        }

        if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
        {
            controllerName = controllerName + "Controller";
        }

        return helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase);
    }

    /// <summary>
    /// Determines if the current request is on the specified controller
    /// </summary>
    /// <typeparam name="TController">The type of the controller.</typeparam>
    /// <param name="helper">The helper.</param>
    /// <returns>
    ///     <c>true</c> if the current view is on the specified controller; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsController<TController>(this UrlHelper helper) where TController : Controller
    {
        return (typeof(TController) == helper.ViewContext.Controller.GetType());
    }
}

نصائح أخرى

أحاول دائمًا تنفيذ الحل الأبسط الذي يلبي متطلبات المشروع.كما قال إنستين ، "اجعل الأمور بسيطة قدر الإمكان ، ولكن ليس أبسط". جرب هذا.

<%: Request.Path %>

لقد نجح هذا بالنسبة لي:

<%= this.Url.RouteUrl(this.ViewContext.RouteData.Values) %>

تقوم بإرجاع عنوان URL الحالي على هذا النحو؛ /Home/About

ربما هناك طريقة أبسط لإرجاع سلسلة المسار الفعلية؟

يمكنك استخدام <%= Url.Action(action, Controller,values) %> لإنشاء عنوان URL من داخل الصفحة الرئيسية.

هل تفعل هذا لتسليط الضوء على علامة تبويب للصفحة الحالية أو شيء من هذا القبيل؟

إذا كان الأمر كذلك، فيمكنك استخدام ViewContext من العرض والحصول على القيم التي تحتاجها.

كتبت فئة مساعد الذي يسمح لي بالوصول إلى معلمات المسار.باستخدام هذا المساعد، يمكنك تمرير وحدة التحكم والإجراء وجميع المعلمات إلى الإجراء.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top