I wrote a UrlHelper and in every view I need to include an using clause:
#using MyWebPage.Helpers
Is there way to avoid it? That would be great if this import automatically.
I added this in web.config as below:
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="MyWebPage.Helpers" />
</namespaces>
</pages>
But it wouldn't solve my problem.
There are 2 web.configs in the project.
For Views
For application
You need to include the namespace in proper web.config which is for views.
Once you include it in proper namespace you don't have to include it in every page.
<system.web.webPages.razor>
<host ....>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="MyWebPage.Helpers" />
</namespaces>
</system.web.webPages.razor>
</pages>
Add the namespace into Views/web.config, close all cshtml files, reopen them, voila.
Related
I developed asp.net MVC 4 site. I had implemented CustomMembership and CustomRole things.
I am getting error while I click on LogOff button
error message:
To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".
this is my Web.Config file.
<membership defaultProvider="MyMembershipProvider">
<providers>
<clear/>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
<add name="MyMembershipProvider" type="MvcApplicationTest.CustomMembershipProvider"
connectionStringName="ApplicationServices"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requireUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLenth ="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow ="10"
applicationName ="MvcApplicationTest.Client.Web"
/>
</providers>
</membership>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
<roleManager enabled="true" defaultProvider="MyRoleProvider">
<providers>
<clear/>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
<add name="MyRoleProvider" type="MvcApplicationTest.Models.CustomRoleProvider"/>
</providers>
</roleManager>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
Thank You,
I have a Mvc4 site that was created eith the empty template. How can I now include forms authentication to the site?
I have checked a few books and shearched but i have not come up with any relevant information.
my webconfig looks like this:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<httpRuntime targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5" />
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<connectionStrings>
//
</connectionStrings>
</configuration>
Go to Web.config file in your project, check if the mode type for authentication is "Forms". See below:
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="20" />
</authentication>
</system.web>
The mode type for authentication have 4 defferent types, like below:
Windows
Forms
Passport
None
If you don't see <authentication> element in the Web.config file, add it inside the <system.web> element. Also because you're using empty templates, you need to add some logic for your Form based authentication. Like create Account Controller and Login Action.
I searched here same problem Asp.net - <customErrors mode="Off"/> error when trying to access working webpage, but when i do like it was told, i dont get any reaction same runtimeerror;
Here is my web.config
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" strict="false" explicit="true" />
<authentication mode="None"/>
<compilation targetFramework="4.5" />
<httpRuntime requestValidationMode="2.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages validateRequest="false">
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
Seems you have the compilation and authentication tag in there 2 times. Remove the duplicates.
Here's my web.config snippet, from the config file that's deeper inside the project than the other.
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="MVCWebsite.Models" />
<add namespace="MVCWebsite.ViewModels" />
<add namespace="MVCWebsite.Helpers" />
<add namespace="MVCWebsite.Extensions" />
<add namespace="Mvc.Mailer" />
All of the above exception .Models are accessible in my views, why can I not access Models? Is there something that prevents me from doing this? I want to access static property on these classes.
How do I access an extension method in an ASP.Net MVC View? In C# I do
using MyProject.Extensions;
and I remember seeing an XML equivalent to put in a view, but I can't find it anymore.
In View:
<%# Import Namespace="MyProject.Extensions" %>
Or in web.config (for all Views):
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Linq" />
<add namespace="System.Collections.Generic" />
<add namespace="MyProject.Extensions" />
</namespaces>
</pages>
For pages using Razor / WebPages, you can include a using directive in your .cshtml page.
#using MyBlogEngine;