ASP.NET Virtual Directory For Common Markup - asp.net

I have a few applications that need to share a common set of markup.
Scenario: I might have www.site1.com, www.site2.com, and www.site3.com. On all sites, /care/contact-us.aspx and /care/faqs.aspx will be exactly the same, but every other page will be totally different.
Issue: I'm attempting to not duplicate the .aspx files for each of these sites and would like to have a /care virtual directory that would include contact-us.aspx and faqs.aspx that each of these sites would use. I have seen this post from Scott Gu, but I'm looking for any other solutions/ideas.
Question 1: What would be the best way to set this up to share the /care directory?
Question 2: Any ideas about also sharing the code behind.
Background, if you care: In a legacy application (asp classic/vbscript), we have the ability to use a /common virtual directory for sites to share common markup and code (since they're all mixed together in .asp files).
Thanks in advance to any help or ideas!

Simply setup a virtual directory in IIS for each of the apps that points to the same physical directory.
Here's a good reference:
http://support.microsoft.com/kb/324785

This is actually pretty hard, and i'd recommend you either bite the bullet and go with the scott gu answer or use the solution we chose, which was to use the svn:externals property within subversion to import a directory from a "shared" repository. Subversion manual reference. If you use a different version control system i would guess it would have something similar but you're on your own in that case.

I use a virtual directory in order to share HTML and Image files between sites. To share ASPX files, things are a bit different - and harder.
When we share HTML files, we do not just link to that file because it would screw up the menu (different sites have different menus - we just want the content of the HTML files). So I created a page (e.g. "ShowContent.aspx") that opens up the HTML file, reads the contents as a string and assigns the string to an ASP label control. This may work for you as-is if you don't generate the content dynamically in your shared ASPX files.
Even if you do, hope is not lost. First, create a project that incorporates JUST the common ASPX files, build it and place the project files in a known location (http://shared.yoururl.com). Now, instead of pulling the contents by accessing a file, simply read the contents off using a WebRequest object:
WebRequest wrContent = WebRequest.Create("http://shared.yoururl.com/CommonInformation.aspx");
Stream objStream = wrContent.GetResponse().GetResponseStream();
StreamReader objStreamReader = new StreamReader(objStream);
string pageContent = objStreamReader.ReadToEnd();
Then display the pageContent on your blank page.
If your common pages are data entry forms then I'm afraid your only hope is to place them in a common source directory that multiple projects share. You'd share the source but would publish the files with each project. This is likely to be messy though: you may make changes in one that break other projects because they now have the possibility of interdependency.

Related

Publishing ASP.NET files to hosting server and back to local machine.Step by step procedures

I am very new with ASP.NET. I need help understanding the basic procedures of how a developer publishes their site to their hosting server, and then once its compiled and published, how does another developer (someone who does not have the original files), edit some of the code behind files.
For example, Developer A creates a site, uploads it to the server and it works great. We hire a new remote developer (Developer B) and he needs to work on a .cs file, does he need to get a copy of the entire site, to his local machine? if so, does he need to reverse the compiling process to get thos .cs files back?
I am a PHP developer, and since theres no compiling needed, I dont worry about .dlls nor about compiling at all. This would truly help me understand ASP.NET with the hopes that it can also help others like me.
Please help. Thank you in advance.
You do not need a copy of the entire site.
But you need the basic struct of an asp.net site, and what resource you may need to run this individual page.
The minimal basic struct of an asp.net site is this two directories (and the content of them - if any)
App_Code
Bin
together with the web.config that is probably needs some changes to run to the remote developer.
Now some simple logic.
If a page have css, javascript and other files you need them too
If a page need connection to some database, you need that too
In the bin directory you may only give the dlls/libraries that this individual page use (if any)
If the page have links to other pages, or post to other pages, or need ajax call from other pages you need them too.
If some one change some functions on the dll files, you must give the update to the remote developer so he can adapt his code.
After the update he can upload the aspx + cs file on the server.

ASP.NET MVC Get image path from different module

For sample I got these two modules.
Module.Admin
Module.Web
Then, all images that are uploaded inside the Module.Admin is place inside it, sitemap goes like this.
Module.Admin
- Content
-- Uploads
--- Images
Now, how can I load as source these images inside the Module.Web > View .cshtml file?
As I've notice, these two modules are having two different ports. I hope you understand what I'm saying. Feel free to ask. Thank you
Sounds like you made the module a web project too? That means that they will run in seperate sites and not be able to share information.
You should probably use plugins instead. I've described how you can do it in my blog: http://blog.gauffin.org/2012/05/griffin-mvccontrib-the-plugin-system/
If you only want to access files you have to setup a custom VirtualPathProvider which can access the files from the other library.

User Controls not reading Resource Files... Organize Resource Files Properly on WebForms .NET?

I'm working on a project right now which has aspx files in the root folder and one folder named "UserControls" that contains all the ascx files. I had to localize it so I used the App_GlobalResources and App_LocalResources for the Resource files. However, I had a problem with the user controls because they're not reading their respective resource files. I started to make some tests and I realized that they only read the resource files if they're placed on an App_LocalResources subfolder inside their respective containing folder and not in the root folder. As I have too many subdirectories with user controls I wouldn't like to place each local resource file on its main containing folder. It just doesn't make sense. Anyone with some ideas? Thanks in advance.
Without looking at your project files, I would guess that the pages that aren't reading their resources from the root App_LocalResources folder are the ones whose filename collides with another page. E.g. if you have /cart/Default.aspx and also /myaccount/Default.aspx, then ASP.NET won't know which one matches /App_LocalResources/Default.aspx.resx. That's why it requires those pages' resource files to be in their own subfolders: to make it obvious which page they match.
I think if you want to avoid this you have three options:
Change everything to Global resources. This is not so bad as long as you make sure the resource names are well organized and it's obvious which page they apply to.
Change the page filenames to be unique. This is pretty ugly as it might break redirects in the app, as well as SEO.
Don't use ASP.NET resources for localization, roll your own solution. Seriously, Resources are one of the many nice, seemingly-useful features in ASP.NET (see also Themes, Mobile support, Roles) that in my experience don't scale well to larger projects, and are best ignored in favor of custom solutions. For example, what happens if your ASP.NET app calls a middle-tier DLL that needs to return localized text? Much better in that case to have your own lower-level resource localization system.
Check that App_LocalResources directory is located in the same folder of the usercontrol.

In which folder can I store user uploaded images

I've a classified ads system on ASP.NET/c#/MS SQL, and I'm trying to figure out where to store the images that people upload when placing an ad. The ad itself is being stored in a SQL server database.
The images are now being stored in a subfolder of my webapp. It seems to work fine, however I only recently discovered a big problem. Everytime a user deletes an ad, the attached images are to be deleted as well including the folder they reside in. This leads to a restart of the asp.net application. I searched internet and found that restarting the web-app is actually intended behaviour when a subfolder is being deleted.
Obviously, I need to fix this. But how to do that? Where can I store images in such a way that:
I can remove these images including the folders they are stored in?
I can acces them using a URL (the images need to be shown in the
webpages)
Without getting the web-app being
restarted?
Any feedback is appreciated!
Paul
See this question Deleting a directory results in application restart
An other alternative would be to store the images in the DB instead.
Another option would be to put the images in a directory completely unrelated to the web site then serve the images through a scripted page or handler. It would make all of your image urls look like mydomaincom/serveimage.aspx?imageid=323422, but unless you're counting on the name somewhere that really shouldn't matter much. Obviously it would require a modification to the page that serves the images in the first place as well, but if sub directories of this unrelated directory are deleted IIS really shouldn't care at all.
maybe you can store the images in SQL (check at the filestream feature in this case)
if not, I suppose you have somewhere in a business facade class, a service class or wherever you want, a methode "DeleteAd".
This method will have to do two things :
-delete the sql data
-delete the file image
also, you may change the image store to another folder, outside the web app. You will probably end with writing a custom handler (myhandler.ashx?fileid=XX) to serve the files, or a custom route and control if you use MVC.

How might i setup my ASP.NET project to find my files?

edit I do not want to redirect pages, specific files etc. I would like to change the path where images, videos and other media are stored from the root source directory to the directory of my choosing. In this case c:/dev/prjfiles/prjname/public (c:/dev/prjfiles/prjname/ is my working directory) and i except when my html does img src="/pic.png" it will find the image in c:/dev/prjfiles/prjname/publi/pic.png. I need a working solution, i tried looking at how to set virtual directories and etc. I cant figure it out. Thus the bounty. I am generating the html, i am not writing asp:image runat="server" etc i am pulling data from a DB and outputing the html. The part that is still a WIP is the code that handles POST request. The html already exist but i cant have hundreds of files in site.com/here pollution my source directory (c:/dev/trunk/thisprj/thisprj/where my .aspx files are and i do not wish 500 .png/gif/jpg here)
I dont know how asp.net environments are usually set up. I am assuming i have a root path that is not available from the web, a bin/ where i may put my asp.net dll and a public where i stick in any files i want.
I would like to have my project files seperated from everything else. My JS, css and image files are in prjfiles/prjname/public with my sqlite db in prjfiles/prjname/ and extra binaries in prjfiles/prjname/bin.
The problem comes when i run my app and try to load an image. Such as /cssimg/error.png. My project does not find resource in my /public folder and i have no idea how to make it find them. How can i set my project up so it does?
NOTE: I set the working directory path so its at prjfiles/prjname/. In code i write ./bin/extrabin.exe and db.sqlite3 which access the files properly.
You might want to watch the getting started videos for ASP.NET
http://www.asp.net/get-started/
EDIT: More info added
As #Murph suggests, your assumptions are incorrect.
IIS takes care of blocking HTTP access to any important files and folders like your *.aspx.cs, and *.cs in the App_Code, any DLLs, anything under the App_Data directory and the web.config.
Content files, such as *.html, *.css, *.js, .gif, .jpg, .png are all served in the normal manner.
In this way, there is no need for a "public" folder.
I dont know how asp.net environments are usually set up. I am assuming i have a root path that is not available from the web, a bin/ where i may put my asp.net dll and a public where i stick in any files i want.
This is wrong assumption!
You have a root folder, which IS available in public. You set IIS or ASP.NEt Development Server to this folder.
(optional, but always needed) You have a web.config file in this root folder for configuration
You have a bin folder for your assemblies (each page or user control "include" compiles to a class)
(optional) You have App_Data as default folder for file-based DBs and/or other data files (say XML storage, ..)
(optional) You have an App_theme folder for styling and images. Read about ASP.NET themes.
(optional) You can add App_Code folder if you want to add classes to be compiled by the server.
You can create folders for scripts, etc...
Normally for complex logic, etc.. you create in a separate project outside the root and reference the result assembly in the bin folder.
Seriously, you cannot do ASP.NET work without an IDE or a manual. Visual Web Developer 2008 Express IDE is free and http://asp.net has tons of resources for getting started.
I don't know if I got the question right, but maybe you could try the <BASE> HTML tag.
HTML <base> Tag
"Specify a default URL and a default target for all links on a page"
There's a nice and simple example at W3Schools, check it out.
The negative side is that you need to put a <BASE> tag in each page you want.
It sounds like you should be able to create a virtual directory to do what you're asking -- but it's a very non-standard setup.
Keep in mind that IIS will prevent users from downloading DLLs and other project-level files, so you usually don't need to partition them off in a separate layer.
For example, just have a cssimg folder at the top level of your project, and skip the whole public folder thing.
I see where you're coming from. ASP.NET projects are set up a little differently from how you're treating them, but you can make them work like you want.
The root of an ASP.NET project IS publicly accessible. When you created your WebSite within Visual Studio, it created a default.aspx page right on the root. Are you hosting in IIS? If so, it's set up to serve up default.aspx by default. But I digress.
Here's how to make it work like you want (mostly):
Create a WebSite, then right-click the site and add a folder named "prjfiles". Right-click that folder and make another named "public". Create another subfolder of that one called "cssimg".
Now, if you want to use the image you mentioned, you'd reference it like this: "~/prjfiles/public/cssimg/error.png" (pathing starting with the root) or "./cssimg/error.png" if you're coming from a page in the public folder (relative pathing).
Really, though, you're doing too much work. Here's how to make it work with less effort:
Create your WebSite, right-click the project and add a folder called "cssimg".
Treat the root as you would the "public" folder- put your pages right there on the root or in subfolders, as needed. You can reference that same image file like this now: "./cssimg/error.png" (relative) or "~/cssimg/error.png" (start from root)
There's also another way to tell the engine where to look for resources, but it's for your css files. Inside the "head" tag, you can add a "style" element (with type="text/css") and inside that you can add something like this: #import '<%= ResolveUrl("~/prjfiles/public/cssimg/styles.css") %>';
Good luck!
If I correctly understood your problem, you're trying to find files which aren't physically stored on a filesystem folder, or stay on a different folder. You can deal with this problems by implementing a UrlRewrite mechanism.
I suggest you to read URL Rewriting in ASP.NET and, after, to take a look into this implementation: A Complete URL Rewriting Solution for ASP.NET 2.0.
If I understand all this correctly (please comment with any correction) right now all your files are together in the root directory and you use <img src="/img.png" /> and it works.
If this is the case, make another directory in the directory the images are in, say call that directory images and put the image files there. now use <img src="/images/img.png" />.
Done.

Resources