{"id":62,"date":"2008-10-17T15:51:29","date_gmt":"2008-10-17T19:51:29","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=62"},"modified":"2008-10-17T15:51:29","modified_gmt":"2008-10-17T19:51:29","slug":"app_code-and-namespaces","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2008\/10\/17\/app_code-and-namespaces\/","title":{"rendered":"App_Code and Namespaces"},"content":{"rendered":"<p>When working in Asp.net, I discovered something interesting about namespace.\u00a0 Before I explain what that is, a little background&#8230;<\/p>\n<p>If a page accesses it&#8217;s masterpage through the typical this.Master way, then any custom methods you&#8217;ve written aren&#8217;t accessible as it as typed as a MasterPage, not your class that inherits it (presumably something like _MyMasterPage).\u00a0<\/p>\n<p>You can get around this easily with something like the following:<br \/>\n_MyMasterPage _myMaster = this.Master;<br \/>\n\/\/now I methods specific to the _MyMasterPage class<\/p>\n<p>I don&#8217;t like repeating that code all over the place, so I usually have a BasePage class that inherits from System.Web.UI.Page and then my pages inherit from it.\u00a0 That way, I can write code like the following:<br \/>\n<code>Public class BasePage<br \/>\n_MyMasterPage _master;<\/code><\/p>\n<p>Private void Page_Load(&#8230;<br \/>\n{<br \/>\n_master = this.Master;<br \/>\n}<\/p>\n<p>Public _MyMaster MasterExtensions<br \/>\n{<br \/>\n\/\/get&#8230;<br \/>\n}<br \/>\n<\/code><br \/>\nNow, each page (that inherits off of BasePage)\u00a0can call MasterExtensions.Whatever() without any setup.\u00a0 And base page can hold similar methods that it&#8217;s handy for all pages to be able to call.\u00a0<\/p>\n<p>Since this is a project with no clutter (all data and logic are in other libraries, I thought I&#8217;d just put the BasePage class(es) in App_Code.\u00a0<\/p>\n<p>That&#8217;s when I hit a problem that rarely comes up.\u00a0 App_Code it&#8217;s in the project root namespace.\u00a0 I had set a default root in the project properties of [ClientCompany].[ProjectName].WebUI.\u00a0 And pages could refer to the BasePage class in App_Code, so you would think that App_Code followed that rule, but it doesn&#8217;t.\u00a0 It builds another namespace, but makes that namespace an automatic import to the rest of the project.\u00a0<\/p>\n<p>So where&#8217;s my problem?\u00a0 I refered to _MyMaster a class that is tied to my specific MasterPage.\u00a0 It&#8217;s in the default namespace.\u00a0 And App_Code doesn&#8217;t get an automatic reference back to project namespace.<\/p>\n<p>Solution: Go into the Base class page and give it a:<br \/>\n<code>using [ClientCompany].[ProjectName].WebUI;<\/code><br \/>\nMost of the time, this doesn&#8217;t come up because classes in App_Code are fairly generic and not tied back to the UI classes.<\/p>\n<p>Another way to solve this (that I&#8217;m probably going to do), is to put an interface to the MasterPage in App_Code.\u00a0 It&#8217;s cleaner anyway, in case you end up with multiple masters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working in Asp.net, I discovered something interesting about namespace.\u00a0 Before I explain what that is, a little background&#8230; If a page accesses it&#8217;s masterpage through the typical this.Master way, then any custom methods you&#8217;ve written aren&#8217;t accessible as it as typed as a MasterPage, not your class that inherits it (presumably something like _MyMasterPage).\u00a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,34,18,24],"tags":[],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-aspnet","category-microsoft","category-programming","category-web"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}