Removing a view from .NET MVC project that is no longer needed - c#

There is a view in my MVC that is no longer needed and I would like to remove it properly beyond just taking away the button that allows users to navigate there.
There are the ActionResult functions in the associated Controller as well, and when I removed these functions as well as deleting the view.cshtml file, I encountered an error when I attempted to deploy the changes because the build was looking for the view.cshtml file that had been deleted.
I'm guessing there is a config file or something similar that has all of the various pages/references included and that is where the deployment is failing.

You need to be sure to delete the view from the project and not just the directory. In VS just right-click on it and delete it.
Also be sure to save your project and that the updated .csproj gets sent to your build server.

the file could possibly still be defined in the .csproj file if you didn't delete it within VS this would still be there and if you didn't deploy the change to the .csproj file also it would still be looking for the file in question

Related

How to create folder in mvc solution directory so that it is included in project?

I am new in mvc and c# and I can't solve following problem:
I am trying to create a folder named "Items" in solution folder.
I have tryed to use CreateDirectory method:
Directory.CreateDirectory("~/Images");
But it didn't work for me - folder wasn't created ..
Partly working solution was to create a folder by :
Directory.CreateDirectory(Server.MapPath("~/Images"));
"Items" folder was created, but it is not included in the solution:
How to create folder in solution directory so that it is included in project ?
(I needs to by done by code not by hand)
You need to understand what solution and csproj file is used for
In general, they're being designed and used for development with Visual Studio, and once the project is compiled, all these files will be ignored and excluded from the deployment package
Directory.CreateDirectory(Server.MapPath("~/Images"));
The code above simply create the directory if not existed yet in the deployment package at run-time, so you won't see it in your solution unless you run the project locally (either debug/release mode, it does not matter here). However, everything will run normally in hosted environment (ex: IIS).
For your information, here's the brief of what solution and csproj is
solution (.sln) file: contains information to manage one or many individual projects, contains build environments (for each project), start up mode (useful when you want to start multiple projects in one run), project dependencies and so on. Take a note that VS also read from suo file (solution user options) which is used to defined user-custom preferences (you should not include the .suo file in the version control, because it's custom settings)
csproj file: define the structures of project, what the namespace is, what is static folders, embedded resources, references, packages, etc.
Lastly, if you create the folder manually, VS will auto include that folder into deployment package AND csproj, but depends on the file type, you might need to change the Build Action and Copy To Output Directory in file properties.
Hope it helps.
A deployed web application on a web server doesn't have any notion of Visual Studio solution or projects. So the Directory.CreateDirectory(Server.MapPath("~/Images")) is the correct way to create a folder inside your web application at runtime but we cannot be talking about including it into a solution because this hardly makes sense in a pre-compiled web application. If you create the directory on your local development machine, you could always manually include the folder to the corresponding .csproj file, but at runtime this will not make any difference whatsoever.
The reason I wanted to create a folder (if didn't exist) was to make sure it exits before I try to store image in it.
After reading posts here and a few google searches I have concluded that the proper way to handle image upload would be
To create (In my case) folder "Images" by hand to be sure it exists
Then storing uploaded img in existing folder:
string path =Server.MapPath("~/Images/"+ UploadedImageName);
file.SaveAs(path);

Publishing website solution - How to stop a deleted file from being regenerated?

I have a website solution in Visual Web Developer. I have replaced a new file with an older file of the same name. I understand that the default behavior for web deploy is to update only older files with new ones. However, in this case I have replaced the new one with the old one by right clicking it (the old one) and clicking 'publish myfile'. This did in fact save the old file as I want.
The problem begins when I later publish the whole website again (to update other files). Somehow - this older file, which I want on the server, is replaced with the newer version - the one that at least theoretically, doesn't exist neither in my files, nor on the server! Where does it come from? And how can I stop that from happening?
I've found a solution, though not why the problem exists.
Open the folder and make a copy of the file. Then, in solution explorer - delete the original. Publish. Rename the copy to the original name. Publish again.
There may be unnecessary steps here. I'm just posting something that worked.

Files show as hidden in Visual Studio 2012

Hei,
So we have a whole group of people working on the same project and every now and then some files show as hidden, you know the blank file icon like obj or bin.
See my font and texture? They should be normal.
So, I can see them in the folder but not in solution explorer without hitting the Show Hidden Files. I can also modify them but I would like them back.
I have been trying to see if there was a solution but all I get is blogs on how to show hidden files.
So I found this thread
Visual Studio 2012 and missing files
that seems to be giving the reason of my problem. People are not saving properly before pushing to github.
Now is there a solution to this?(apart from saving properly) Can I make all of those files become normal files?
Cheers
Simply right click the files/folders and include them to source control again.
This usually happens if people check in new files/folders but do NOT check in the updated project file, too. The project file contains a list of all files which should be included within the solution. If this runs out of sync, exactly this crap will happen... ;)
Simply instruct your folks to properly check-in. Or do code review on check-in and verify that the project/solution file(s) are also checked in.
They show up as hidden because they're not part of the project/solution, but they are on your file system.
You can right-click the files and click Include in Project
If this does not solve the issue after checking in. The project file is what is not properly getting checked in.
Probably the problem is that they are not pushing the project file (file with extension .csproj). It holds all references to files included on the project so it needs to be pushed when new files are added.
Let everyone know that they need to push this file in order to everyone to catch latest changes on project.

Altering 1 C# controller file

I've worked with PHP but never C#, and I need to make 1 change to a file, and was hoping I could get some guidance, or suggested resources to learn more.
I have 1 cs controller file where I want to change the [Authorize(Roles="A")] to [Authorize(Roles="A,B")]
I made the change but nothing happens, so I've come to the conclusion that this cs file probably needs to be compiled.
The question is, is there a way to compile this one file? Or does the entire site need to be compiled?
When I open the cs files, it opens with "Visual Studio Tools For Application 2.0 2008".
Is this sufficient to make the changes?
My thinking is that one of the following needs to happen:
Scenario 1:
Open the .cs file in the appropriate compiler (of which I do not know
what it is) Compile it and copy the newly compiled file into the
appropriate folder.
Scenario 2:
Open the whole project in a compiler - of which I'm guessing the
".csproj" file to open. Compile the whole project Copy the desired
compiled file to replace the old file.
Scenario 2b:
Open the whole project in a compiler - of which I'm guessing the
".csproj" file to open. Compile the whole project Copy everything
back to the server.
Scenario 3:
The code I have is not sufficient to do the changes, and I need some
other source code.
That's all I've got, if someone can suggestion which one(s) may be on the right track, and how I may go about doing this, it would be greatly appreciated.
Thanks
The fact you are talking about a Controller would strongly suggest you are working on an ASP .NET MVC project. You will have a .sln file which is a solution file which is the master document if you will:
YourWebsiteSolution.sln
YourWebsiteProject.csproj
YourController.cs
Once you open the .sln file with Visual Studio (you need to know what version but you can get the latest here: Download VS2012 Web Express). You can the choose Build -> Build -> ReBuild Solution from the menu and that will re-compile all of your .cs and associated files into DLLs that will be in your \Solution\Project\bin\Debug folder (or Release depending on build).
When you deploy to the server you only deploy the .DLL files (in this instance). You NEVER deploy the actual .cs files as they contain the code and you don't want people peeking into them (this differs from PHP where the code is on the server).
Because you have not changed any of the views you don't need to follow this step but for a full deploy you would also copy any .cshtml or .aspx files and the usual suspects such as .css and .js etc to same folders on server as on the project.
I hope that is a good starter to get you going?

Add created class file to project

I have a console application that builds some default classes for me from a database. When the files are built, I want to be able to refresh my folders and see the new files in my class library.
However no matter what I do the files don't show up unless I go in and manually add existing files. Is there a way for VS2010 to look at the file folder and add in anything that is in that folder to the project? For example:
Folder > File1.cs, File2.cs, File3.cs, File4.cs
VS2010 sees
Folder > File1.cs
How can I make VS2010 show these new classes?
Your problem is that you will only see files that are included and referenced in your .csproj file. This is by and large a good thing because it gives you ultimate control over what is taken into account in the project or not. This is causing you a problem though, because the created files which are inserted into your project directory aren't being referenced. As you have mentioned you can include the files manually, but I understand that you wish this process to be automatic.
The best way to resovle this in my opinion is instead of having a project create the files, use design-time T4 templates. Design-time T4 templates are files which resemble pre-Razor ASP.NET views, which allow code generation within your project. You can access your database, format your classes and then output .cs files directly into your project without building it. This is extremely convenient becuase it lets you work on catching compile-time errors that may come up based on the output without having to do a complete build.
More information about using T4 can be found here.
And a good walkthrough can be found here.
Haven't tried this personally, but you should be able to do it using this..
First gain a reference to your project using your apps' solution, then with the Visual Studio automation framework (DTE):
ProjectItems p = Project.ProjectItems;
p.AddFromFile("File1.cs");
Taken from: http://msdn.microsoft.com/en-us/library/envdte.projectitems.addfromfile.aspx
I would read further into it.
Select the project where you can find your file
On top of your solution explorer you can select "show all files"
Select your files and include
Adding them automatically can be done from another app or script by modifying your projects .csproj/vbproj file
<Compile Include="My Project\MyClass.vb" />
This must be done in the correct itemgroup.
I think this is not directly possible. You may write a template file (t4) in order to create you cs files and they will be added to project when the transformation file is run.
In order to run the transformation file after / before build, you may write a pre/post build event.
That will require you to create a VS add-in.. you can find an example here...
Okay so I have a console application that is building some default classes for me from a database.
Can't you let this application write all classes in one file, say Proxy.cs or Entities.cs. Then every time you regenerate the file and rebuild the project, you can access the new classes.

Categories

Resources