Posts Tagged ‘aspnetmvc’

Compile aspnet mvc views (*.cshtml)

Jan 26th, 2014

To make a long story short one can compile the views in asp net mvc.

It takes considerable time though so I only activate it for release compilations.

Update the build file with


1
<MvcBuildViews Condition=" '$(Configuration)' == 'Release' ">true</MvcBuildViews>

Like so:


1
2
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<MvcBuildViews Condition=" '$(Configuration)' == 'Release' ">true</MvcBuildViews>

I am sorry about all &gt; and &lt; but I don’t have the time right now to make everything look pretty. Just replace &gt; with < and &lt; with > and your good to go.
Or follow the links below with the awesome formatting Stack overflow provides.

Honour those who should. Especially this answer that does the only-release trick.

Exception: Uncaught reference: Model is not defined – have you forgotten the ampersand?

Jul 22nd, 2012

In Aspnet MVC we use “Model” to send  data to the view/cshtml file.

Then we want to use this Model in Jquery.

One cannot simply write

$("#MyControl").text( Model.ID );

Instead write

$("#MyControl").text( "@Model.ID" );
I wouldn’t this either but instead send the data to a hidden control or something.  I can’t formalise why but the feeling is with me that I shouldn’t.
Read slightly more here and here.

Exception in AspnetMvc with Razor: Parser Error Message: Expected “}”.

Apr 28th, 2012

The below text/error can occur if you have missed one of the } in jquery in a @section features{ section.

It can be hard tracking exactly which } is missing because commenting out the javascript code doesn’t change anything – it seems the razor engine doesn’t care about that type of comments.  Instead start the @section{} anew and copy-paste the code in again piece by piece.

In my case it was a totally ok javascript for loop:

for (i = 0 ; i < 12 ; i++) { }

Event though it is correct syntax it makes Razor fail.  Seems to be a bug in AspnetMVC/Razor/Dotnet4.5/Visualstudio11.
I am losing karma by not making the bug easy to reproduce and report the bug.

Server Error in ‘/myApplication’ Application. 

 

Parser Error 

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Expected “}”.

Source Error:

Line 204:        </p>

Line 205:    </section>

Line 206:</section>

 

Aspnetmvc 4 and standard dotnet membership and roles

Oct 26th, 2011

I recently added the aspnet way of handling membership and roles to an experimental site.

One should not write one’s own usr/pwd handling since there are too many places to go wrong.

The MS tutorials all take for granted we want our users in a sqlexpress database situated as app_data in the web project.  For most real solutions we want to have the users in a database of our own pick.  Pointing aspnet to this one is not hard but has some caveats.

Update your database either through aspnet_regsql.exe found in the dotnet framework folder.  This application creates a bunch of stored procedures in the database you chose.  If you don’t want everything – say you only want roles and membership but not personalization or schema you have to run aspnet_regsql.exe with parameters.  You can also run some scripts to do the same, the same scripts that aspnet_regsql.exe uses.

Then you have to point your site (web.config) to use the database.  There is already a default membership provider in machine.config and we want to override it.

Create your ususal connection string.

<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString=...;MultipleActiveResultSets=True" />
  </connectionStrings>
...

Then to point at it scroll down to (aspnetmvc template has already created it) membership and update.  Check the bold for clues.
“Clear” is used for overriding the machine.config value.  And since you override it you have to provide a new; that is where “defaultProvider” comes in so set it to the same value as “name”.  Finally  “connectionStringName” must be equal to your connectionstring’s name as above.

    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <clear />
        <add connectionStringName="DefaultConnection"              ...
             name="DefaultMembershipProvider"               ...
         />
      </providers>
     </membership>

Then you probably want Roles to also be handled in your own database so do the same for “roleManager”.

All this can be found on the web but I spent an hour on it so I thought I’d jot it down for future googling.

Sources:
http://computerscribe.com/blogs/BlogPost?id=11
http://www.asp.net/security/tutorials/creating-the-membership-schema-in-sql-server-cs
http://forums.asp.net/t/978442.aspx/1 

Exception: Unable to cast object of type ‘ASP._Page_Views_Home_Index_cshtml’ to type ‘System.Web.IHttpHandler’.

Mar 27th, 2011

This exception might have to do with the browser calling http://localhost/Views/Home/ instead of http://localhost/Home in an aspnetmvc project.

Also make sure there is a HomeController in /Controllers.

I haven’t delved into this since it just was an obstacle in something else I was playing with.