How to change file name in Visual studio project? - c#

I have a project solution in visual studio. Its created with MVC .net
I want to move some of my code files such as index.cshtml and others out of the Views>Home folder and into only Views folder. Will this break my project?
Also I want to rename index.cshtml to a new name, throughout all instances in my solution. When I tried this it only did the one file and it broke the project. I tried a CTRL F to find all index.cshtml, but it said there are no instances (though I know there are)
I need to rename the files correctly, so that I can deploy my project to match a current setup.
How do I rename all instances of index.cshtml in visual studio ?
And will moving the files outside of the home folder break the project?
I tried this it only did the one file and it broke the project. I tried a CTRL F to find all index.cshtml, but it said there are no instances (though I know there are)

If you just starting out I'd recommend not moving/renaming view files by hand. Eventually you'll have good understanding of how view discovery works and will be able to move files to places where they can be found at run-time (and even add more places yourself).
ASP.Net is convention-based approach to locate view files - short version is by default runtime looks for .cshtml files with this name format "/views/{controller name}/{action name}.cshtml", you can change view name by specifying different name when returning View("MyOtherView") from controller.
For detailed information on how view discovery works see Order in which Views are searched in MVC, how to change order of search viewLocation in asp.net mvc?, Microsoft - Views in ASP.NET Core MVC.

Related

Where can I find information on the conventions of razor page loading?

I can't find (by string search) any references to the files in my Pages/Shared folder anywhere in my project, but if I remove the folder and its files my test page stops loading correctly. Clearly some kind of implicit convention is at work, but I'm unversed on this matter. Where can I find the relevant information on this?
Files in the Pages/Shared folder are used for reusable parts. The reference, as it is shared throughout the app, is made only by filename or relative path from shared subfolder and without using extension.
You should have in shared folder _Layout.cshmtl and _Layout.cshtml.cs at least. That file is defining layout that is used by default on every page you create.
Once you search for "_Layout" in you project you should get at least one result.
Information about project files for Razor pages are available in Examine the project files and about the layout you can find in Layout in ASP.NET Core.
That might give you an idea how it works by default or what is purpose of said folder and file(s).
You can also read and follow instructions in Understand Searched Locations For The Razor View Engine And ASP.NET to get a bit more understanding how it all works by default.

What is build action property of a cs file in c#?

What is different type of build action and how it affect our code
Why would .cs files default Build Action be "Content"? It seems like "Compile" would be more appropriate.
edit: I created a test .cs file (Class) in the App_Code folder for my test and I am using Visual Studio 2015 (fully updated). This is using the default Web Application template (MVC) in Visual Studio.
edit2: I just did another test and the same thing happened. Maybe the cause is the project type I am using (ASP.NET MVC Web Application). It seems like the App_Code folder is the recommended place to put cs files, however, since nothing from this folder can be served to the client. I haven't made any changes to VS2015's configuration or anything like that.

Serving .cshtml files on IIS globally

I am trying to setup intranet IIS 8.5 (Win8.1) to globally serve .cshtml (Razor) files. The corresponding Application Pool is set to v4.0.
The files are simple Web Pages, not MVC. Here is an example of one:
<html>
<body>
#foreach (var i = 0; i < 10; i++)
{
<li>Item #i</li>
}
</body>
</html>
By removing the mapping of .cshtml files to System.Web.ForbiddenHandler on the IIS server's Handler Mappings I was able to get past the initial hurdle of ASP.NET telling me that
This type of file is not served.
However, the .cshtml files are now served verbatim to the browser, instead of being run through the Razor rendering process.
One would think that it should be easy to serve Razor pages from IIS, but it isn't. I need to somehow convince IIS to interpret these pages as Razor views; I suspect I am missing some mapping to the appropriate handler (I don't want MVC though - just simple Web Pages).
Here are some additional constraints:
I would definitely like to avoid including a bin folder with the requisite Razor assemblies in each of the sites on the server. The server hosts many sites, and I don't want to have to copy the bin folder everywhere. It should be possible to configure it globally, once and for all.
Ideally, I would not even need a local web.config for each site. The sites that are being served are a patchwork of technologies, containing .html, .shtml, .php, .asp, .aspx, and - hopefully - .cshtml files and should not be dependent on a single technology or config.
Creating a Visual Studio project is expressly out of the question. I should be able to use any text editor to modify the .cshtml files.
.NET Core is not installed on the machine and is not an option. Must use full Framework up to 4.6.2.
I am aware of many other SO questions that are similar, but don't quite solve my problem.
This question for example, was closed as "unclear" before it could have been answered, yet it was pretty clear to me! I am having the exact same problem.
The accepted answer to this question simply resorts to copying the bin folder. This is something I specifically don't want to do.
This answer says you can run an MVC application without installing MVC on your server, again by copying a bin folder into the local root. I do want to install Razor (but not necessarily MVC) onto my server globally.
Essentially, what I am trying to do is to use Razor syntax in a way reminiscent of classic ASP, or ASPX, without the baggage of MVC.
Can it be done?
I believe what you're looking for is a feature called ASP.NET Web Pages. If you use ASP.NET Core 2.0, the most recent new project templates uses Pages, rather than controllers and views. I've never read this doco, but I guess it should help you get started (or just create an ASP.NET Core 2.0 project from the new project wizard)

.NET (Visual Studio) Share assets between projects

I'm working with Visual Studio. There I have a solution with several web-projects (.net MVC 4). All of these web-projects use the same javascript-libs. Currently I copied the library into each project, but this can't be the final solution. What is the best approach to share those library (assets in general) between all of the projects? Just link them? Or is it possible to create a project and reference it in all projects?
Update
"Link" the javascript files from another project is not a possible solution as I would have to link thousands of files (one of the libraries I am using is ExtJs) what makes it impossible to build a project without freezing visual studio...
Possible solution
Currently I have a (Web) MVC Project called "Web" and a (Class Library) Project called "ClientScript" which contains all the JavaScript files which are shared between several Web Projects. As linking all the needed JavaScript files is not a possible solution (because it's a matter of thousands of files what causes visual studio to freeze) I copy all the needed JavaScript files to the individual Projects using the Build Events in each Web Project (Project -> Properties -> Build Events -> Post-build).
My Post-build command line in the Web Project looks like this:
start xcopy "$(SolutionDir)ClientScript\Frontend\*" "$(SolutionDir)Web\Scripts" /r /s /i /y /D /E
Every time you build your Web Project all the changed Javascript files get copied from the ClientScript Project to your Web Project.
While developing the Javascripts I run a small "filewatcher" tool which automatically copies a file from the ClientScript Project to every Web Project when it changes. This way I don't have to build the Web Project every time when I make a change to one of the Javascripts.
Anyone that stumbles across this question here in the future should know that there are now Shared Projects in Visual Studio to solve this problem. Universal Windows projects use them by default and you can create your own by downloading and installing the VS extension here: https://visualstudiogallery.msdn.microsoft.com/315c13a7-2787-4f57-bdf7-adae6ed54450
Note: At this time they can be picky about what type of project you try to add the shared reference. I created a JavaScript shared project to share js files between a Windows store js app and an MVC web app and it would not let me do that saying they had to be of the same language. (It supports C#, C++, or JavaScript).
Place the JS files in a single folder, likely above all others, and add them to the project but use the "Link" option. It's part of the drop down on the "OK" button in the "Add existing item..." dialog.
When you run every new ASP.NET MVC 4 project it's take a new port then other app have take.
I simply suggest you a simple thing.
run a project which contain all the pacakages. open them webmatrix and run them as localhost:80.
You need to set the port in settings section of your site in webmatrix. Now it will rechable at localhost now you can reference all the libraries from this packages.
Slightly older thread, but I have another way of doing a similar thing using Web Essentials, that handles the issue of not publishing correctly.
I have a shared folder outside of the projects that require the shared file, normally a 'common' project with other things in as well, but can be just a simple folder as suggested by Michael Perrenoud.
However instead of 'Add as Link' I have been creating a new bundle in the project that requires the shared js/css file, with the same name as the shared file, and then referencing that file in the shared folder using a relative reference location rather than the root based one it starts with.
To add a file from a shared folder in the root of the solution to the scripts folder use the following code in a new bundle file (*.bundle), changing the folder/file names as required.
<?xml version="1.0" encoding="utf-8"?>
<bundle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://vswebessentials.com/schemas/v1/bundle.xsd">
<settings>
<minify>false</minify>
<runOnBuild>true</runOnBuild>
</settings>
<files>
<file>../../MySharedFolder/my-js-file.js</file>
</files>
</bundle>
Then every time you build it recreates the bundle with the latest version, this version is then also published as expected :)
You can even create a minified version if desired by changing 'minify' to true. Or better yet you can add them loads as a bundle too if you want, you have that flexibilty.
This is an older thread but due to complex business requirements these days applications are divided in to different modules or sub projects.Thus, brings us the need to share common resources like JavaScript files, themes and CSS style sheet files.
I personally feel that common files should be put in separate Asp .Net MVC 5 project which has following structure :ASP.NET MVC5 folder structure
Now the best part is you can separately manage the dependencies using Bower,NPM or Nuget package manager.
After you have organised all the files in this project host this project to your own CDN or may be on cloud. You can use Using CDN in Bundle Approach to get script or link references.
That will help you sharing common resources across all the projects.There us a short coming though if you have many developers on the team and if someone added incompatible version lib can affect all the apps.

Visual Studio Cannot Find .cs

I am just starting to learn ASP.NET (C#) and I am struggling with a tutorial I am working on.
I have my App_Code Folder and within it, I have a folder called DataAccessStuff. Within this folder are a bunch of .cs files I will be using for the data layer of things.
The problem is, Visual Studio is unable to detect the location of these .cs files and gives me the error:
"The type name "DataAccessStuff does not exist in the type 'System.Web.UI.WebControls.Content"
I have no idea how to get it to recognize. I simply copied the project file from the textbook CD onto my computer as it said. I have attached images showing the precise tree layout of the project as well as the code I am using. If anyone can please help a beginner out, it would be appreciated.
Thanks!
Inside one of the .cs files
The namespace is probably
Content.DataAccessStuff
based on the root name of your project. However, if you simply double click the file in DataAccessStuff that you want to access, it will give you the namespace of the file.
EDIT: Since this is a Web Site Project, not a Web Application Project, namespacing is by default not explicit unless added.
Make sure anything referencing something belonging to the folder Content.DataAccessStuff has a
using Content.DataAccessStuff;
at the top of the class with the rest of your using statements.
The following article does an excellent job highlighting some of the key differences between Web Site Projects and Web Application Projects.
http://msdn.microsoft.com/en-us/library/dd547590.aspx
Check the namespace provided in all the class (.cs files) of 'DataAccessStuff' folder.
It should be 'Content.DataAccessStuff'.
write using lab6.DataAccessStuff instead of Content.

Categories