Visual Studio MVC template creation issue - c#

I am attempting to create a custom ASP.NET MVC4 template. I start from the Basic MVC4 template, make my modifications, and then use the "Export Template" wizard to create the template zip file. Right now (almost) everything is working smoothly. When I use the template to create a new MVC application, it recreates all of my settings the way I want them except one. For some reason, it changes the project properties for my web application to have a Start Action of "Current Page" instead of "Specific Page" (like it was in the original template and like it is in my template). This setting is in the project properties under the Web tab. Here is what it is set to in my template application (before I generate the actual template zip file):
And here is what it is like when I create a new project using that template:
How do I modify my template to set this setting properly (or more accurately, how do I force it to remember what I set initially)?

Edit: answer revised due to misunderstanding of both original request and behavior of previously proposed solution.
To set the Start Action of a custom MVC project template, you'll need to create a dll with a class that implements the Microsoft.VisualStudio.TemplateWizard.IWizard interface. To use the wizard dll, you'll either need to copy it to Visual Studio's probing path, which is (VS2010 Install Dir)\Common7\IDE, (VS2010 Install Dir)\Common7\IDE\PrivateAssemblies, or (VS2010 Install Dir)\Common7\IDE\PublicAssemblies. If you don't put the compiled dll in one of those directories, you'll need to strong name and sign the dll and add it to the GAC and get the publickeytoken of the dll and add it to the Assembly element in the vstemplate file.
In testing the following code I copied the dll to (VS2010 Install Dir)\Common7\IDE\PrivateAssemblies, so the dll is not signed.
Wizard code
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
namespace WarrenG.StartAction {
public class Wizard : IWizard {
private readonly Dictionary<string, object> data = new Dictionary<string, object>();
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary,
WizardRunKind runKind, object[] customParams) {
if (replacementsDictionary.ContainsKey("$wizarddata$")) {
string xml = replacementsDictionary["$wizarddata$"];
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode node in doc.ChildNodes) {
data.Add(node.Name, node.InnerText);
}
}
}
public bool ShouldAddProjectItem(string filePath) {
return true;
}
public void RunFinished() {
}
public void BeforeOpeningFile(ProjectItem projectItem) {
}
public void ProjectItemFinishedGenerating(ProjectItem projectItem) {
}
public void ProjectFinishedGenerating(Project project) {
if (data.ContainsKey("WebApplication.DebugStartAction")) {
project.Properties.Item("WebApplication.DebugStartAction").Value =
data["WebApplication.DebugStartAction"];
} else {
project.Properties.Item("WebApplication.DebugStartAction").Value = 1;
}
}
}
}
Add wizard specific elements to vstemplate file of custom MVC project template
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateContent>
<!-- various template content -->
</TemplateContent>
<!-- add the following -->
<WizardExtension>
<Assembly>WarrenG.StartAction, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null</Assembly>
<FullClassName>WarrenG.StartAction.Wizard</FullClassName>
</WizardExtension>
<WizardData>
<WebApplication.DebugStartAction>1</WebApplication.DebugStartAction>
</WizardData>
</VSTemplate>
The start actions on the project page appear to be numbers 0 through 4, following their display order. A value of 1 corresponds with Specific Page.

Unfortunately, or fortunately, depending on the side of the coin you're on...
Like the "Startup Project" setting, that setting is NOT part of the project file or the template file that's generated. It is stored in the "SUO" or Solution User Options, file. The SUO is not included by the template generator.
Some background on the SUO file: http://msdn.microsoft.com/en-us/library/bb165909(v=vs.80).aspx

Related

Is it possible to get a class and it's properties within a visual studio item template?

I am currently creating a few templates for my team to use within various projects.
I have a very simple "simple mapper" template and a wizard to go along with it that I'd like to expand adding others as I continue.
namespace $rootnamespace$;
public class $safeitemname$
{
public $mapto$ Map($mapfrom$ entity)
{
return new $mapto$()
{
Id = entity.Id,
Name = entity.Name
};
}
public $mapfrom$ MapToDb($mapto$ item)
{
return new $mapfrom$()
{
Id = item.Id,
Name = item.Name
};
}
}
$mapto$ is replaced with the user's input.
I would like to know if it's possible to look through the hosts solution, get the properties of a class referenced there (e.g. $mapto$) and use this to default the mapping. Is this possible with item templates?
I have seen some reference to T4 templates and will be looking in to those, but would like to know if it's possible with the basic item templates.
I tested in VS2022 by adding CustomParameter element into vstemplate file for the template and it can get the properties of a class referenced in current project or current solution.
After you create an item template, the files for the template are added to a .zip file.The default location is %USERPROFILE%\Documents\Visual Studio \My Exported Templates.
Edit the vstemplate file and add custom parameters into the file.
example
<TemplateContent>
<References />
<ProjectItem SubType="" TargetFileName="$fileinputname$.cs" ReplaceParameters="true">MapTest.cs</ProjectItem>
<CustomParameters>
<CustomParameter Name="$mapto$" Value="user’s input : the class you want to reference "/>
<CustomParameter Name="$mapfrom$" Value=" user’s input : the class you want to reference "/>
</CustomParameters>
</TemplateContent>

Resource files (resx) does not respect Custom Tool Namespace

In the resx properties, I changed the Custom Tool Namespace from DefaultNamespace to MyNamespace.Language and the following code is generated:
namespace MyNamespace.Language
{
public class CommentResources
{
public static global::System.Resources.ResourceManager ResourceManager {
get {
//removed code...
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DefaultNamespace.CommentResources", typeof(CommentResources).Assembly);
}
}
As you can see, only the class namespace is changed, but not the namespace passed in the ResourceManager constructor and because of that, when I instanciate ResourceManager(typeof(CommentResources)) and try to access a key, it throws MissingManifestResourceException, for example:
var manager = new ResourceManager(typeof(CommentResources));
var resource = manager.GetString("myKey");
How can I truly changed the namespace?
EDIT:
Take a look at my solution below. Whenever I create a resx file within Enviroment folder, it creates a unwanted namespace. That's what's I'm trying to avoid
I recently stumbled to the same issue.
It seems that Visual Studio 2017 generates code that creates ResourceManager from RootNamespace.SubFolder.ResourcesFileName instead of CustomToolNamespace.ResourcesFileName.
Since the code is created from the Visual Studio's Single-File Generator tool called either ResXFileCodeGenerator (for internal class) or PublicResXFileCodeGenerator (for public class) that internally uses StronglyTypedResourceBuilder class, there is no way to customize its behavior, other than implementing your own Single-File Generator as a Visual Studio extension and using it as a Generator for the EmbeddedResource.
Fortunately, there is a simpler workaround. In .csproj file, under EmbeddedResource tag, specify LogicalName tag with text value of RootNamespace.SubFolder.ResourcesFileName.resources.
In your specific case, it would look like this:
<LogicalName>DefaultNamespace.CommentResources.resources</LogicalName>

I can't read my embedded XML file from my second C# project

I have a Visual Studio Solution with two projects inside.
The first project is a windows service, and this project contains one XML file inside a folder (called Configurations). This XML file is called Databases.xml. I have changed Databases.xml Build Action from content to embedded resource, and now I want to access this XML file from my other project in my solution, which is a WPF application.
I have therefore added an reference to my windows service project inside my WPF project, and would now like to access my XML file from my WPF project.
My problem is that when I am trying to access the embedded resource then I can't find out which type to use and what the path/namespace to my assembly and XML file should be. When I am using the
string[] names = this.GetType().Assembly.GetManifestResourceNames();
my names array is filled out with some resources from my WPF project. What I want is to access the ResourceNames and of course my Databases.xml file from my Windows Service project.
Please help me since this problem is driving me nuts.
If you need any additional information, please let me know.
My Solution Update 26-07-2013
I found out that the real problem occured when I couldn't use my first windows Service projects namespace as a type for my assembly. my Windows Service consists of a service class (with OnStart() and OnStop() method inside), and in order to use this class as my namespace type, I needed to add another reference to my WPF project. I needed to add a reference to System.ServiceProcess namespace, in order to use my Windows Service Class as a type for my assembly in my WPF Project.
In Order to access my Databases.xml file, I have come up with this solution. Remember to insert your own projects name and class name instead of my placeholders (<Windows Service Project Name> etc).
//Remember to add a reference to System.ServiceProcess in order to be able to use your WIndows Service Project as an assembly type.
using (Stream stream = typeof(<Windows Service Project Name>.<Windows Service Class Name>).Assembly.GetManifestResourceStream("<Windows Service Project Name>.<Folder Name>.Databases.xml"))
{
//Load XML File here, for instance with XmlDocument Class
XmlDocument doc = new XmlDocument();
doc.Load(stream);
}
So my real problem was that I didn't include the System.ServiceProcess reference to my second project.
You've to refer to Windows Service project Assembly to make it work
The problem with your code is This.GetType().Assesmbly gives current Assembly In your case WPF Assembly and obviously you'll not find what you need there.
Try this
Assembly windowsServiceAssembly = typeof(SomeTypeFromThatAssembly).Assembly;
string[] names = windowsServiceAssembly.GetManifestResourceNames();
Hope this helps.
If your class is static class then use this methods:
internal static string GetFromResources(string resourceName)
{
var asm = Assembly.GetExecutingAssembly();
var resource = asm.GetManifestResourceNames().First(res => res.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase));
using (var stream = asm.GetManifestResourceStream(resource))
{
if (stream == null) return string.Empty;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
For example if your embedded resource file names on the this project is 'MyFile.txt' then use this static method same this code:
var myFileData = GetFromResources("MyFile.txt");

Setup project , Custom Installer, Connection string

I'm working on a setup project. In a separate library project I created a custom installer by inheriting from System.Configuration.Install.Installer and added the generated .dll as a custom action.(Install step). I added an app.config file to the library project where I store a connection string which I need to make a connection to Sql Server.
Once I run the setup project, the custom installer doesn’t retrieve the connectionString stored in the app.config file.
Where can I store the connection string? Can a setup project have an app.config? Could somebody recommend a book on deployment/setup projects?
Thanks
UPDATE
Hi,
Thanks. Based on the replies I've updated my code this is what I´m doing now:
-> In setup project, I added a custom action to the install step, selecting application folder and primary output(library project).
-> Added an app.config to the library project and set its build action to "content".
-> Added the library project content files to the setup project.
This way the app.config file appears in the install folder.
Within the install handler from my custom install class I do the following: (in this case I access the application settings)
string appPath = "";
appPath = Path.Combine(new DirectoryInfo(Context.Parameters["assemblypath"].ToString()).Parent.FullName, "App.config");
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = appPath;
System.Configuration.Configuration c = null;
c = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
string result = c.AppSettings.Settings["test"].Value;
You can still reuse app.config if you insist on doing so, but you need to come up with your own way to parse it.
Here is the necesary steps for this workaround:
Create your own mechanism to
read the appSettings using
System.Xml.XmlReader or whatever from
the app.config file that will be
copied into the Application Folder.
Use your custom xml parser to
extract the value and use it in your
custom action.
Mark
App.config as Content in the Build Action property
window (in the custom action project).
In the Setup Project, add Project Output in the Application Folder and choose Content Files
instead of Primary output. You can
verify that app.config is the output
by invoking context menu and choosing
Outputs for the Content Files from
... item.
After you've done this, you should have 2 outputs from the Custom Installer class library project (1 for Primary output and 1 for the Content Files).
Here is an example custom installer action that will launch whatever I have in my appSettings key called launch along with my replacement ConfigurationManager implementation:
using System;
using System.Configuration.Install;
using System.Xml;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
namespace ClassLibrary1
{
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
public Installer1()
{
InitializeComponent();
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
System.Diagnostics.Process.Start(ConfigurationManager.AppSettings["launch"]);
}
}
public class ConfigurationManager
{
private static AppSetting _appSettings = new AppSetting();
public static AppSetting AppSettings
{
get { return _appSettings; }
}
public class AppSetting
{
public string this[string key]
{
get
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), "app.config");
var xpath = string.Format("/configuration/appSettings/add[#key='{0}']", key);
var doc = new XmlDocument();
doc.Load(path);
var node = doc.SelectSingleNode(xpath);
if (node == null)
return string.Empty;
return node.Attributes["value"].Value;
}
}
}
}
}
Instead of trying to parse the app.config, I would suggest adding the configSource attribute to your app.config to externalize the connection strings, something like:
<connectionStrings configSource="connections.config" />
Then get the setup script to generate the entire connections.config file, similar to:
<connectionStrings>
<clear />
<add name="...." connectionString="...." providerName="...." />
</connectionStrings>
Make sure you use .config as the extension of your file so it cannot be served to the browser if someone guesses the file name.

Editing a custom configuration section in an installer class

I am trying to update a custom configuration section of a web.config file during the installation of my product in a custom action. I wanted to use the actual configration classes to do this however when the installer runs it loads my installer class but then the
Configuration.GetSection throw a File Not Found exception as it is trying to load my custom section class from the windows system directory. I managed to get this to work by copying the required assemblies into the windows system directory but this is not an ideal solution as I cannot guarantee I will always have access to that directory.
How else can I solve this problem?
My update code looks like this
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public override void Install(System.Collections.IDictionary stateSaver)
{
//some code here
webConfig = WebConfigurationManager.OpenWebConfiguration("MyService");
MyCustomSection mySection = webconfig.GetSection("MyCustomSection") //<--File Not Found: CustomConfigSections.dll
//Update config section and save config
}
}
My config file looks like this
<configuration>
<configSections>
<section name="myCustomSection" type="CustomConfigSections.MyCustomSection, CustomConfigSections" />
</configSections>
<myCustomSection>
<!-- some config here -->
</myCustomSection>
</configuration>
Hope you would understand the answer the way it is intended.
Assuming that you have setup the installer to have your project output. If Not
Right Click on installer Project click add->Project Output->select your project
and then you can continue using your code.
Moreover if you are using dll except the .net Ones make sure to change there
properties to copylocal = true
If You want to read the element Before Installation use BeforeInstall Event
Handler and try reading your file. ihope your problem will be solved
If in case You want to read the element after installation Right Click On
installer project Click view->customActions->On Install Click Add Custom Action
->Select Application Folder -> Select Primary output from your project and click
ok .
Now Click on primary output and press F4 and in Custom Action Data write
/DIR="[TARGETDIR]\"
and after that write your code as follows.
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
public ProjectInstaller()
{
this.InitializeComponent();
}
private void InitializeComponent()
{
this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall);
}
void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
string path = this.Context.Parameters["DIR"] + "YourFileName.config";
// make sure you replace your filename with the filename you actually
// want to read
// Then You can read your config using XML to Linq Or you can use
// WebConfigurationManager whilst omitting the .config from the path
}

Categories

Resources