2011-09-21 12 views
10

मेरे पास यह मार्ग परिभाषित है:Querystring के बजाय URL में पैरामीटर दिखाने के लिए ActionLink?

routes.MapRoute(
        "Details", // Route name 
        "{home}/{details}/{id}/{name}", // URL with parameters 
        new 
        { 
         controller = "Home", 
         action = "Details", 
         id = UrlParameter.Optional, 
         name = UrlParameter.Optional 
        } // Parameter defaults 
       ); 

एक्शनलिंक:

 @Html.ActionLink("Show Details", "Details", "MyController", new { id = 1, name ="a" }) 

/Home/Details/1?name=a में एक्शनलिंक परिणाम /Home/List/1/a

के बाद

उत्तर

12

आपका मार्ग परिभाषा इस तरह होना चाहिए:

routes.MapRoute(
    "Details", // Route name 
    "{controller}/{action}/{id}/{name}", // URL with parameters 
    new 
    { 
     controller = "Home", 
     action = "Details", 
     id = UrlParameter.Optional, 
     name = UrlParameter.Optional 
    } // Parameter defaults 
); 

इसके अलावा, आप का उपयोग करना चाहिए proper overload:

@Html.ActionLink(
    "Show Details",    // linkText 
    "Details",     // action 
    "MyController",    // controller 
    new { id = 1, name = "a" }, // routeValues 
    null      // htmlAttributes 
) 

सूचना अंत में null

+1

actionlink ठीक काम किया जब ऊपर मार्ग डिफ़ॉल्ट मार्ग से पहले जोड़ा गया है। हालांकि, डिफ़ॉल्ट अब/होम/विवरण – Danny

1

एक जंगली अनुमान:

शायद आपका मार्ग डिफ़ॉल्ट मार्ग के बाद पंजीकृत था। इसे अपने ग्लोबल.एक्सएक्स के अंदर पहले रूट के रूप में रखें, फिर यह काम करेगा।

नीचे की तरह:

public static void RegisterRoutes(RouteCollection routes) { 

     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
          "Details", // Route name 
          //Put action instead of details 
          "{home}/{action}/{id}/{name}", // URL with parameters 
          new 
          { 
           controller = "Home", 
           action = "Details", 
           id = UrlParameter.Optional, 
           name = UrlParameter.Optional 
          } // Parameter defaults 
         ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

    } 

अद्यतन

@Simon सही है, लेकिन अगर आप चाहते हैं यदि आप किसी अन्य तरीके से उपयोग कर सकते हैं।

केवल एक क्रिया विधि के लिए काम करने के मार्ग के लिए, निम्न कोड का उपयोग करें।

इस प्रकार की कोई समस्या बनाएँ:

public class EqualConstraint : IRouteConstraint { 

    private string _match = String.Empty; 

    public EqualConstraint(string match) { 

     _match = match; 
    } 

    public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { 

     return string.Equals(values[parameterName].ToString(), _match); 
    } 
} 

और फिर नीचे की तरह अपना रूट बदलें:

routes.MapRoute(
         "Details", // Route name 
         //Put action instead of details 
         "{home}/{action}/{id}/{name}", // URL with parameters 
         new 
         { 
          controller = "Home", 
          action = "Details", 
          id = UrlParameter.Optional, 
          name = UrlParameter.Optional 
         }, // Parameter defaults 
         new { 
          controller = new EqualConstraint("Home"), 
          action = new EqualConstraint("Details") 
         } 
        ); 
+0

पर जाता है Darin – Danny

+0

@Danny पर मेरी टिप्पणी देखें "डिफ़ॉल्ट अब/होम/विवरण" पर आपका क्या मतलब है? – tugberk

+0

जब एप्लिकेशन प्रारंभ होता है तो यह डिफ़ॉल्ट रूट/होम/इंडेक्स – Danny

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