2008-09-30 6 views
18

क्या नियंत्रक कार्रवाई या दृश्य पर जुड़े मार्ग/वर्चुअल यूआरएल को प्राप्त करना संभव है? मैंने देखा कि पूर्वावलोकन 4 ने LinkBuilder.BuildUrlFromExpression सहायक जोड़ा है, लेकिन यदि आप इसे मास्टर पर उपयोग करना चाहते हैं तो यह बहुत उपयोगी नहीं है, क्योंकि नियंत्रक प्रकार अलग हो सकता है। किसी भी विचार की सराहना की जाती है।Asp.Net एमवीसी: मैं वर्तमान नियंत्रक/दृश्य के लिए वर्चुअल यूआरएल कैसे प्राप्त करूं?

उत्तर

14

आप 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()); 
    } 
} 
+0

चेतावनी: यह कोड नहीं रह गया है MVC5 में नियंत्रकों के बीच डाटा साझा करने के लिए कैसे फिर 5. – qJake

+0

@qJake ASP.NET MVC में काम करता है? – Mrunal

+0

@Mrunal यह प्रश्न नियंत्रकों के बीच डेटा साझा करने के बारे में नहीं है, बल्कि नियंत्रक के साथ समेकित मार्गों को देखने के तरीके के बारे में है। मुझे पता है कि एएसपी.नेट एमवीसी 5 (अब एएसपी.नेट कोर) के साथ यह संभव है, लेकिन मुझे सिंटैक्स ऑफहैंड नहीं पता है। – qJake

3

आप <% = Url.Action (कार्रवाई, नियंत्रक, मान) का उपयोग कर सकते%> मास्टर पृष्ठ के भीतर से यूआरएल बनाने के लिए।

आप शायद करने के लिए यह कर रहे हैं वर्तमान पृष्ठ या कुछ और के लिए एक टैब पर प्रकाश डाला?

यदि ऐसा है तो आप दृश्य से ViewContext का उपयोग करें और मूल्यों आप की जरूरत हो सकता है।

1

मैं a helper class ने लिखा है कि मुझे मार्ग मापदंडों का उपयोग करने की अनुमति देता है। इस सहायक के साथ, आप नियंत्रक, कार्रवाई, और कार्रवाई के लिए पारित सभी पैरामीटर प्राप्त कर सकते हैं।

18

यह मेरे लिए काम किया:

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

यह इस तरह के रूप वर्तमान यूआरएल रिटर्न; /Home/About

हो सकता है कि वहाँ वास्तविक मार्ग स्ट्रिंग वापस जाने के लिए एक सरल तरीका है?

22

मैं हमेशा परियोजना की आवश्यकताओं को पूरा करने वाले सबसे सरल समाधान को लागू करने का प्रयास करता हूं। जैसा कि एन्स्टीन ने कहा, "चीजों को यथासंभव सरल बनाएं, लेकिन आसान नहीं।" इसे इस्तेमाल करे।

<%: Request.Path %> 
+0

धन्यवाद, आसान और सरल +1 – Deano

संबंधित मुद्दे