ASP.NET MVC: How can I add views, scripts and cont

2019-06-16 08:51发布

I've got a common Layout.cshtml file that I want to share. When I add the file as a link in the project (via Add Existing Item > Add as Link) I get an error that it cannot be found when I run the application. If I set the build action to "Copy Always" it does copy the file to the build directory, however I still get an error that it cannot find the file (Perhaps I can somehow instruct VS to copy it to the appropriate directory?).

I came across this question: ASP.NET 2 projects to share same files however the answers are pretty complicated and ugly and right now I'm playing around with a lot of different structures and refactoring methods so I don't want to go through some big production just to move a few different files around.

So how can I easily share these kinds of files between the two applications?

Thanks!

3条回答
在下西门庆
2楼-- · 2019-06-16 09:08

Looks like it's a bug in msbuild or an unsupported feature. Either way I finally found a workaround published on this guy's blog:

http://consultingblogs.emc.com/jamesdawson/archive/2008/06/03/using-linked-files-with-web-application-projects.aspx

查看更多
唯我独甜
3楼-- · 2019-06-16 09:27

Someone else might give you a better solution, but my approach would be to:

 1 - Create a library project
 2 - Create a Resources file in the library project
 3 - Add the shared resources to the library project.
 4 - Compile the library project
 5 - Reference the DLL for the library project in the dependent projects 
查看更多
我命由我不由天
4楼-- · 2019-06-16 09:32

You cannot Add as Link static files in an ASP.NET web application. All files need to be included inside this application or when you deploy it in IIS simply won't be able to find any of them. A common technique of sharing static files between multiple web applications is using CDN (Content Delivery Network) where you would host all your static resources (images, CSS, js, ...). For example Google already provides popular frameworks on their CDN.

So for all your custom images you could setup a domain on your web server which will host them all and your web applications will point to this common location. Another advantage of this technique is that clients visiting those applications will already have all static resources in cache.

查看更多
登录 后发表回答