Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An <a href="/questions/tagged/asp.net-mvc" class="post-tag" title="show questions tagged &#39;asp.net-mvc&#39;" rel="tag">asp.net-mvc</a> application has something called a <em>Route Table</em>. The <em>Route Table</em> maps particular URLs to particular controllers. An application has one and only one <em>Route Table</em>. This <em>Route Table</em> is setup in the <a href="/questions/tagged/global-asax" class="post-tag" title="show questions tagged &#39;global-asax&#39;" rel="tag">global-asax</a> file. In case you don't have <em>Route Table</em>, application will always give 404 error.</p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } </code></pre> <p><strong>Case -1</strong>. <em>Consider a case when you have Area in your application. But, you did not mention the Area name in the URL. Example</em> <strong>localhost/ControllerName/ActionName</strong>. <em>If you Pay attention to the URL, there is no Area Name. Also there is no such Controler in the Root Controller Directory. This Controller exists in the Area. Let's say you have following Area in your application.</em></p> <p><img src="https://i.stack.imgur.com/CkQgT.png" alt="Area"></p> <p>Now you type the url : <strong>localhost/Test/Index</strong> and hit enter. Now the question is what is happening in background?</p> <p>Let's added a Reference of <strong><a href="http://haacked.com/archive/2011/04/13/routedebugger-2.aspx" rel="nofollow noreferrer">RoureDebugger</a></strong>. You can get the Dll from this location. Then I added a line of code in my <a href="/questions/tagged/global-asax" class="post-tag" title="show questions tagged &#39;global-asax&#39;" rel="tag">global-asax</a> file under <em>Application_Start</em> Handler.</p> <pre><code>RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); </code></pre> <p>When I execute the application. This shows me following output, instead of Index page response.</p> <p><img src="https://i.stack.imgur.com/pOIJN.png" alt="RouteDebugging"></p> <p><em>If you pay attention to the above screenshot, Default Route is enabled which is present in the <a href="/questions/tagged/global-asax" class="post-tag" title="show questions tagged &#39;global-asax&#39;" rel="tag">global-asax</a> file. It's because default Url without constraint will locate the Controller anywhere in the application. But the View is neither present in the Root Folder View's Directory nor present in the Shared Folder View's Directory. Finally you will get 404 error.</em></p> <p><strong>How can I get rid of this issue ?</strong></p> <p>I faced the similar issue in the past and found the corrective action. below is the new version of default Route in <a href="/questions/tagged/global-asax" class="post-tag" title="show questions tagged &#39;global-asax&#39;" rel="tag">global-asax</a> file.</p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); var route = routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults new[] { "MvcApplication1.Controllers" } ); route.DataTokens["UseNamespaceFallback"] = false; } </code></pre> <p><em>In the above code I added Namespace in the constraint and <code>route.DataTokens["UseNamespaceFallback"] = false;</code> in the route. After doing this and using the above mentioned URL, Controller Action Method will never be executed because Area Name is not present in the Controller.</em></p> <p>Also I figured out one more important information regarding the DataTokens when the Url does not contains the Area Name and Default Route does not contain the NameSpace information. below is the screenshot.</p> <p><img src="https://i.stack.imgur.com/Z8YTq.png" alt="DataToken Without NameSpace"></p> <p><em>The Data Token information will not be available when the Url does not contains the Area Name and Default Route does not contain the NameSpace information.</em></p> <p><br><br><br> Now, after adding the NameSpace and adding the Area Name in the Url as per the revised Default Route. We will get the DataToken Information as shown below in the screen shot.</p> <p><img src="https://i.stack.imgur.com/LEXvq.png" alt="Data Token with NameSpace"></p> <p><br><br><br></p> <p>Below is the <code>RouteData</code> Output after adding the Area Name in the Url</p> <p><img src="https://i.stack.imgur.com/hUT6s.png" alt="Route Debugging"></p> <p>As you can see the Area Route is enabled now.</p> <p>Hope this will help you. Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload