Razor view finding ViewModel without full path - asp.net

Still new to ASP.NET, so forgive me for the simplicity of my question.
I'm writing a demo app in ASP.NET MVC Core 2. For my Views, I'm referencing the
full path to the ViewModel, for example:
#model MyApp.ViewModels.ProductDetailViewModel
A solution that I'm following along with is using some voodoo whereby it's referencing its ViewModels without the full path:
#model ProductDetailViewModel
I don't mind using the full path, I'm just curious as to how the demo project is doing it. I'm not seeing any obvious clues as to how it's done.

This likely is because the demo project will have added the namespace. It's done in the web.config, usually inside the Views folder. There will be a line like this:
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="MyApp.ViewModels" /> <!--- this line
For ASP.Net Core, the import is done in the _ViewImports.cshtml file which is in the Views folder. Add a line like this:
#using MyApp.ViewModels

Related

Is there a way to tell ASP.NET app where to look for a static main html

I have an ASP.NET WebAPI project and I have an AngularJS front-end in the same project. Right now, I have Index.html in the root of the project, and it works fine: when I enter something like localhost:8080, I can see the contents of my Index.html. However, I'd like to move it to an other folder, out of the root folder. Is there a way to config the application to look for a static startup Html file in a specific location? I'd like to avoid using MVC controllers just to point to the right file (I'd like to keep them out of my app).
I don't have any configuration for MVC in my Global.asax (that is no MVC RouteConfig) because I don't want to use MVC in my application because that's to be taken care of by Angular. Angular gets loaded from my Index.html. I just want to move my Index.html to Angular folder to keep it all in one place.
I am pretty sure there was a way to configure that stuff in web.config in the WebForms days, but I don't remember anything about that.
I found the answer to my question. Web.config needs this:
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear/>
<add value="SomeDir/SomeHtml.html"/>
</files>
</defaultDocument>
...

How to use kendo-ui asp.net mvc in visual studio 2015?

I want to use kendo-ui asp.net mvc in visual studio 2015. I install telerik package and the kendo-ui added to my visual studio.
I create a new kendo-ui asp.net mvc project. When i open index.cshtml file and write
#{Html.Kendo().DatePicker().Name...}
the inheritence dose not show Kendo() method and i cant use it.
How can i create Controls such as a DatePicker in kendo-ui 2016 and visual studio 2015?
Check your Views\Web.config. It should have like:
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
...
<add namespace="Kendo.Mvc"/>
<add namespace="Kendo.Mvc.UI"/>
</namespaces>
</pages>
</system.web.webPages.razor>
or add #using Kendo.Mvc.UI to index.cshtml
The first thing to check when you don't see something in the intellisense, is your project "References". It is the only place the compiler looks to find the used libraries. In your case you need a reference to Kendo.Mvc.dll as described here.
Next, it's worth noting that Html.Kendo() is an "Extension Method" and is simply a shorthand for Kendo.Mvc.UI.HtmlHelperExtension.Kendo(Html). So, when you have the required reference, the latter form should work; But to be able to use the shorter form, you need to import the Kendo.Mvc.UI namespace in your cshtml file. There are two ways to do this:
Add #using Kendo.Mvc.UI to the top of the cshtml file, or
Add this line: <add namespace="Kendo.Mvc.UI"/> to the namespaces section in Views\web.config in your project. This imports the namespace for every cshtml file under the Views folder, but you might need to reload the project for this to work.
Anyway, please follow this guide to be sure you won't forget anything.

Web.config add namespace not applied at file level

I am referencing a class library in a web application project (both in same solution). Within the web pages of the web application, if I do this:
If MyValidation.CorrectEmailFormat(email) Then ...
...Visual Studio 2013 underlines the method, and suggests I import MyCompany.EmailMethods at the top of the page. If I do the Import, the page compiles and the method works okay.
However, because these methods are used extensively across the application, I don't want to add them at page level every time. So I headed for web.config, and did this:
<pages>
<namespaces>
<add namespace="MyCompany.EmailMethods" />
</namespaces>
</pages>
However, VS is still prompting me to perform the Import at the top of every page, and the method is not recognised in the page without doing this. What I am doing wrong please? I assumed from MSDN and other sources this was the correct way to achieve this.
Web application is ASP.Net web pages (4.6).
The reference must be added to the Imported Namespace as described in the following SO post
add-a-namespace-reference-to-all-web-application-web-pages-in-a-different-project
It must be added in the project properties page at the bottom part titled Imported Namespaces
The <pages> directive applies to ASPX files only.
You need to use the equivalent directive for Razor:
<system.web.webPages.razor>
<pages>
<namespaces>
<add namespace="MyCompany.EmailMethods" />

How to alter CSS with web.config file

I have been asked to create a visual contrast for an HTML form to distinguish it from a production environment and a development environment, using a web.config file.
My exposure to ASP.NET is very limited. Although I've been researching web.config files since yesterday, due to my lack of experience in this technology, I still don't think I can provide a solid solution with much confidence.
If this job could be handled directly with HTML, CSS, PHP or JS I could do it myself, but using web.config is a requirement.
The goal is to create an unmistakable visual change (let's say, lightgreen background) when a user is on the development form.
There is already a CSS stylesheet in play, so I'm thinking the change would have to go inline in the HTML to override the external styles.
So here's the question, if it makes any sense:
If this is in the production web.config: <add key="CurEnv" value="Production"/> and this is in dev: <add key="CurEnv" value="Development"/>, how can I set the form's background color based on the value in the config file?
This is how I do it:
For DEV, for example, I have the following key in my appSettings:
<appSettings>
<add key="skin" value="skin-gray"/>
...
</appSettings>
And then in my Master page (if using Web Forms) or on my _Layout.cshtml (if using MVC), I have the following HTML:
<body class="#System.Configuration.ConfigurationManager.AppSettings["skin"]">
The syntax for WebForms would be:
<body class="<%=System.Configuration.ConfigurationManager.AppSettings["skin"]%>">
For Production, I have a "skin-blue" setting in my appSettings section and that way, when I deploy the app, depending on the environment, the skin is automatically changed.
I actually do this automatically, as part of the Build process using Slow Cheetah
So that depending on which Configuration is chosen to build the application, the right value is used. This is my transform for Web.Production.Config:
<appSettings>
<add key="skin" value="skin-blue" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
I think you can read those link. It's may help you:
http://forums.asp.net/t/1349824.aspx?How+to+set+CSS+color+settings+from+web+config
Dynamically setting CSS values using ASP.NET

VB webform in IronPython asp.net website

I tried to bring a previously done webform made in vb.net to an IronPython asp.net website with no luck. After seeing it didnt work, I tried to write the simplest codebehind vb.net webform to see if there was a problem with vb.net in an IronPython website and I got the following usual error
"be sure that the defined class in this file matchs with the one in the attribute inherits and that it extends the right base page (page or control)" (sorry if the translation isnt the most accurate I get that message in spanish)
but if I create a vb.net webform in the same website, with the sourcecode in the same file (with the vb.net code between script runat="server" tags in the same page) I get no problem.
Do I have to configure something for both kind of sourcecode languages to run in such way in the same IronPython website, like configuring something in the webconfig file or is there some compatibility issue for doing that which can't be resolved?
The code between <script /> tags is compiled dynamically when the page is first run. This enables you to mix languages. However, the classes in your code-behind files are statically compiled into an assembly by VS.NET ... and a VS.NET project can only support one language at a time.
One solution is to put your VB.NET code-behinds in a separate assembly. For example:
Add a new VB Class Library project to your existing solution
Add a reference to System.Web
Create your VB.NET code-behinds. They should be normal classes inheriting from System.Web.UI.Page.
In your ASP.NET website project, add a reference to the new project
Edit the # Page directives in your *.aspx files to inherit the classes in the new project
e.g. <%# Page Inherits="YourNewVBClassLibraryProject.MyVBCodeBehinds" ... /> where the Inherits attribute contains the relevant namespace-qualified class name
Thanks for the reply Serilla. Your information was interesting but I simply solved it by creating the app_folder and adding the vb files there. Do you think I could have some future problem for doing so?
The problem with the vb files was when these lines in the web.config were enabled for Ironpython to work
<pages compilationMode="Auto" pageParserFilterType="Microsoft.Web.Scripting.UI.NoCompileCodePageParserFilter" pageBaseType="Microsoft.Web.Scripting.UI.ScriptPage" userControlBaseType="Microsoft.Web.Scripting.UI.ScriptUserControl">
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
when I removed them, vb code behind files worked but ironpython didnt. When the lines were there, Ironpython code behind files worked but vb ones didnt

Resources