<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>johna's blog</title>
<link>https://johna.compoutpost.com/</link>
<description>...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.</description>
<item>
<title>ASP.NET Web Pages cshtml files not recompiling on server</title>
<link>https://johna.compoutpost.com/blog/1184/asp-net-web-pages-cshtml-files-not-recompiling-on-server/</link>
<description>I have an old website written in ASP.NET v4.x Web Pages v1.0.0.0 (not Web Forms or MVC).&lt;br&gt;&lt;br&gt;When I upload changes to my shared hosting provider some cshtml files update fine and I can see the changes, but some do not. They seem to be stuck and won't recompile on the server.&lt;br&gt;&lt;br&gt;The problem files are suspiciously all in the same folder.&lt;br&gt;&lt;br&gt;If I delete the updated file I get a 404 error as expected but even when I reupload the file I see the old version in my browser.&lt;br&gt;&lt;br&gt;I download the file and checked that it was the right version, which it is.&lt;br&gt;&lt;br&gt;I also tried renaming the folder in case it was a reserve folder name, but no change.&lt;br&gt;&lt;br&gt;I have tried recycling application pool, restarting site and changing web.config to force it to update but nothing works. I also tried various options for fcnMode in web.config but no change. It is shared hosting so I can't restart the server or IIS.&lt;br&gt;&lt;br&gt;The only thing that worked for me was moving the files to a different existing folder. That seemed to work but I don't know why.</description>
<comments>https://johna.compoutpost.com/blog/1184/asp-net-web-pages-cshtml-files-not-recompiling-on-server/#comments</comments>
<pubDate>2020-08-18T12:00:00+10:00</pubDate>
<category>ASP.NET Web Pages</category>
<guid>https://johna.compoutpost.com/blog/1184</guid>
</item>
<item>
<title>Disabling request validation in ASP.NET Web Pages</title>
<link>https://johna.compoutpost.com/blog/1115/disabling-request-validation-in-asp-net-web-pages/</link>
<description>&lt;img alt=&quot;Disable Request Validation in Web Pages&quot; src=&quot;/blog/uploads/img1115_disable-request-validation-in-web-pages.jpg&quot; class=&quot;img-fluid&quot; /&gt;&lt;br&gt;&lt;br&gt;Disabling request validation in ASP.NET Web Pages&lt;br&gt;&lt;br&gt;If you are developing ASP.NET Web Pages (not Web Forms or MVC) and you need to allow users to submit HTML or HTML-like content in input boxes you will find that the methods to avoid the &lt;strong&gt;A potentially dangerous Request.Form value was detected from the client&lt;/strong&gt; error that work for ASP.NET Web Pages and MVC don't work for ASP.NET Web Pages.&lt;br&gt;&lt;br&gt;For Web Pages you need to change the way you retrieve the submitted data and use the &lt;strong&gt;Request.Unvalidated()&lt;/strong&gt; method.&lt;br&gt;&lt;br&gt;These are the methods for retrieving submitted values and how you should change them to allow HTML data:&lt;br&gt;&lt;br&gt;&lt;pre&gt;Request[&quot;key&quot;] //will not allow HTML&lt;br&gt;Request.Unvalidated(&quot;key&quot;) //will allow HTML&lt;br&gt;&lt;br&gt;Request.Form[&quot;key&quot;] //will not allow HTML&lt;br&gt;Request.Unvalidated().Form[&quot;key&quot;] //will allow HTML&lt;br&gt;&lt;br&gt;Request.QueryString[&quot;key&quot;] //will not allow HTML&lt;br&gt;Request.Unvalidated().QueryString[&quot;key&quot;] //will allow HTML&lt;/pre&gt;&lt;br&gt;&lt;br&gt;If you are using ASP.NET Web Forms you can make a change in the web.config and/or indivudual pages to allow HTML values to be submitted.&lt;br&gt;&lt;br&gt;On the individual page level you can add &lt;strong&gt;validateRequest=&quot;false&quot;&lt;/strong&gt; to your &lt;strong&gt;&amp;lt;@ Page ... %&amp;gt;&lt;/strong&gt; directive in your &lt;strong&gt;.aspx&lt;/strong&gt; file(s).&lt;br&gt;&lt;br&gt;If targetting .NET framework 4 you will also need to add &lt;strong&gt;&amp;lt;httpRuntime requestValidationMode=&quot;2.0&quot; /&amp;gt;&lt;/strong&gt; to the &lt;strong&gt;system.web&lt;/strong&gt; section of the web.config file:&lt;br&gt;&lt;br&gt;You can also disable request validation for all pages by adding &lt;strong&gt;&amp;lt;pages validateRequest=&quot;false&quot; /&amp;gt;&lt;/strong&gt; to the &lt;strong&gt;system.web&lt;/strong&gt; section of the web.config file.&lt;br&gt;&lt;br&gt;eg.&lt;br&gt;&lt;br&gt;&lt;pre&gt;&amp;lt;configuration&amp;gt;&lt;br&gt;  &amp;lt;system.web&amp;gt;&lt;br&gt;   &amp;lt;httpRuntime requestValidationMode=&quot;2.0&quot; /&amp;gt;&lt;br&gt;   &amp;lt;pages validateRequest=&quot;false&quot; /&amp;gt;&lt;br&gt;  &amp;lt;/system.web&amp;gt;&lt;br&gt;&amp;lt;/configuration&amp;gt;&lt;/pre&gt;</description>
<comments>https://johna.compoutpost.com/blog/1115/disabling-request-validation-in-asp-net-web-pages/#comments</comments>
<pubDate>2019-11-27T12:00:00+10:00</pubDate>
<category>ASP.NET Web Pages</category>
<image>https://johna.compoutpost.com/blog/uploads/img1115_disable-request-validation-in-web-pages.jpg</image>
<guid>https://johna.compoutpost.com/blog/1115</guid>
</item>
<item>
<title>Using WebMatrix.Data in ASP.NET Web Forms</title>
<link>https://johna.compoutpost.com/blog/726/using-webmatrix-data-in-asp-net-web-forms/</link>
<description>&lt;a href=&quot;http://www.asp.net/web-pages&quot; target=&quot;_blank&quot;&gt;ASP.NET Web Pages&lt;/a&gt; (you know the difference between ASP.NET Web Forms and Web Pages, right?) has a simple to use Namespace that help you open, query and send commands to a database, and to work with rows that are returned by SQL queries.&lt;br&gt;&lt;br&gt;Although intended for use in ASP.NET Web Pages and WebMatrix, it can also be added to ASP.NET Web Forms websites and applications and used as a simpler alternative to System.Data.SqlClient, or as an alternative to Linq to SQL, Entity Framework, or the many ORMs and Micro ORMs available.&lt;br&gt;&lt;br&gt;Why use WebMatrix.Data?&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;If you want to craft your own SQL queries or access stored procedures, rather than use Linq.&lt;/li&gt;&lt;li&gt;If you usually use System.Data.SqlClient but would like something simpler to use.&lt;/li&gt;&lt;/ul&gt;Why wouldn't you use WebMatrix.Data?&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;You need or prefer strongly typed objects, and Intellisense.&lt;/li&gt;&lt;li&gt;You're afraid people will laugh at you when they see you used it.&lt;/li&gt;&lt;/ul&gt;Here's some examples of how simple WebMatrix.Data can be to use.&lt;br&gt;&lt;br&gt;&lt;b&gt;Open a database&lt;/b&gt;&lt;br&gt;&lt;br&gt;You can open a database by specifying a SDF database file, a connection string from web.config, or a connection string.&lt;br&gt;&lt;br&gt;&lt;pre&gt;var db = Database.Open(filename);&lt;/pre&gt;&lt;br&gt;Or&lt;br&gt;&lt;br&gt;&lt;pre&gt;var db = Database.Open(connectionStringName);&lt;/pre&gt;&lt;br&gt;Or&lt;br&gt;&lt;br&gt;&lt;pre&gt;var db = Database.OpenConnectionString(connectionString);&lt;/pre&gt;&lt;br&gt;&lt;b&gt;Execute a command and return a count of affected rows&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;int rows = db.Execute(&quot;INSERT INTO Data (Name, DateStamp, Status) VALUES ('Smith', DateTime.Now, null)&quot;);&lt;/pre&gt;&lt;br&gt;&lt;b&gt;Get the identity columns from the most recently inserted row&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;int id = db.GetLastInsertId();&lt;/pre&gt;&lt;br&gt;(Note that under the hood this method uses &quot;SELECT @@Identity&quot;. See &lt;a href=&quot;http://stackoverflow.com/questions/7753037/how-to-get-inserted-row-id-with-webmatrix&quot; target=&quot;_blank&quot;&gt;this Stack Overflow quesion&lt;/a&gt; for some alternative methods.)&lt;br&gt;&lt;br&gt;&lt;b&gt;Query the database and return a collection of rows&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;foreach (var result in db.Query(&quot;SELECT * FROM PRODUCT&quot;))&lt;br&gt;{&lt;br&gt;	int id = result.Id;&lt;br&gt;	string name = result.Name;&lt;br&gt;	var price = result.Price;&lt;br&gt;	bool? enabled = product.Enabled;&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;b&gt;Query the database and return a single row&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;var product = db.QuerySingle(&quot;SELECT * FROM Product WHERE Id = 1&quot;);&lt;br&gt;&lt;br&gt;if (product != null)&lt;br&gt;{&lt;br&gt;	int id = product.Id;&lt;br&gt;	string name = product.Name;&lt;br&gt;	var price = product.Price;&lt;br&gt;	bool? enabled = product.Enabled;&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;b&gt;Query the database and return a scalar value&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;int count = db.QueryValue(&quot;SELECT COUNT(*) FROM Product&quot;);&lt;/pre&gt;&lt;br&gt;As you can see in the above examples you can explicitly specify the column type value, or use var.&lt;br&gt;&lt;br&gt;&lt;b&gt;Passing parameters&lt;/b&gt;&lt;br&gt;&lt;br&gt;Execute, Query, QuerySingle and QueryValue all allow you to add parameters very simply.&lt;br&gt;&lt;br&gt;For example:&lt;br&gt;&lt;br&gt;&lt;pre&gt;db.Query(&quot;SELECT * FROM PRODUCT WHERE Price &gt; @0 AND Price &lt; @1&quot;, 20, 100))&lt;/pre&gt;&lt;br&gt;Importantly, passing parameters this way protects against SQL injection attacks.&lt;br&gt;&lt;br&gt;&lt;b&gt;Closing the database&lt;/b&gt;&lt;br&gt;&lt;br&gt;As usual, it's good practice to close your connection when you're finished with it. You can do this with a using block or explicity.&lt;br&gt;&lt;br&gt;&lt;pre&gt;using (var db = Database.Open(filename))&lt;br&gt;{&lt;br&gt;	//code here&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;Or&lt;br&gt;&lt;br&gt;&lt;pre&gt;db.Close();&lt;br&gt;db.Dispose();&lt;/pre&gt;&lt;br&gt;&lt;b&gt;How to add a reference to WebMatrix.Data&lt;/b&gt;&lt;br&gt;&lt;br&gt;In Visual Studio 2012 you will find WebMatrix.Data under Assemblies then Extensions.&lt;br&gt;&lt;br&gt;&lt;img alt=&quot;Adding a reference to WebMatrix.Data&quot; src=&quot;/blog/uploads/img726_vs2012.jpg&quot; class=&quot;img-fluid&quot; /&gt;&lt;br&gt;&lt;br&gt;For detailed documentation see &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/webmatrix.data(v=vs.111).aspx&quot; target=&quot;_blank&quot;&gt;WebMatrix.Data Namespace&lt;/a&gt;.</description>
<comments>https://johna.compoutpost.com/blog/726/using-webmatrix-data-in-asp-net-web-forms/#comments</comments>
<pubDate>2013-07-17T12:00:00+10:00</pubDate>
<category>ASP.NET Web Forms</category>
<category>ASP.NET Web Pages</category>
<image>https://johna.compoutpost.com/blog/uploads/img726_vs2012.jpg</image>
<guid>https://johna.compoutpost.com/blog/726</guid>
</item>
<item>
<title>Installing ASP.NET Web Pages Helpers in Visual Web Developer 2010</title>
<link>https://johna.compoutpost.com/blog/586/installing-asp-net-web-pages-helpers-in-visual-web-developer-2010/</link>
<description>The documentation for ASP.NET Web Pages (Razor) is good if you are using WebMatrix, but there are some shortcomings if you are using Visual Web Developer or Visual Studio.&lt;br&gt;&lt;br&gt;One example is the Helpers that are mentioned in the documentation: for Google Analytics, Twitter, Facebook, etc.&lt;br&gt;&lt;br&gt;It is not explained how to add this functionality in VWD or VS 2010.&lt;br&gt;&lt;br&gt;Here's how to do it... In &lt;i&gt;Solution Explorer&lt;/i&gt; right-click on your project and click &lt;i&gt;Add Library Package Reference&lt;/i&gt;. This brings up a dialog where you can search online for packages. The one you are looking for is &lt;i&gt;microsoft-web-helpers&lt;/i&gt;. Search for it and click &lt;i&gt;install&lt;/i&gt;.&lt;br&gt;&lt;br&gt;In this dialog you may find some other interesting packages that you can install. You can also view these packages on &lt;a href=http://www.nuget.org/&gt;www.nuget.org&lt;/a&gt;. There is a JQuery package which adds Intellisense for JQuery, amongst other functionality. There is a PayPal Helper. Or you may find the JQuery client-side form validation helpful.</description>
<comments>https://johna.compoutpost.com/blog/586/installing-asp-net-web-pages-helpers-in-visual-web-developer-2010/#comments</comments>
<pubDate>2011-05-21T12:00:00+10:00</pubDate>
<category>ASP.NET Web Pages</category>
<guid>https://johna.compoutpost.com/blog/586</guid>
</item>
<item>
<title>Connecting to MySQL Database with ASP.NET Web Pages (Razor)</title>
<link>https://johna.compoutpost.com/blog/585/connecting-to-mysql-database-with-asp-net-web-pages-razor/</link>
<description>To connect to a MySQL database from ASP.NET Web Pages (Razor) you need to do the following:&lt;br&gt;&lt;br&gt;1. Add a &quot;Bin&quot; ASP.NET folder to your project and copy the file &lt;i&gt;MySQl.Data.dll&lt;/i&gt; in to it.&lt;br&gt;&lt;br&gt;You can obtain this file by downloading the latest version of the MySQL Connector/NET (&lt;a href=http://dev.mysql.com/downloads/connector/net/&gt;http://dev.mysql.com/downloads/connector/net/&lt;/a&gt;), installing it, then locate the file in the installation folder.&lt;br&gt;&lt;br&gt;2. In your &lt;i&gt;web.config&lt;/i&gt; file add a connection string for your database, for example:&lt;br&gt;&lt;br&gt;&lt;pre&gt;&amp;lt;configuration&amp;gt;&lt;br&gt;    &amp;lt;connectionStrings&amp;gt;&lt;br&gt;        &amp;lt;add name=&quot;Any-name&quot; connectionString=&quot;Server=server-name-or-ip;Port=3306;Database=database-name;Uid=username;Pwd=password;&quot; providerName=&quot;MySql.Data.MySqlClient&quot; /&amp;gt;&lt;br&gt;    &amp;lt;/connectionStrings&amp;gt;&lt;br&gt;&amp;lt;/configuration&amp;gt;&lt;/pre&gt;&lt;br&gt;3. In your code you can then open a connection to your database:&lt;br&gt;&lt;br&gt;&lt;pre&gt;var db = Database.Open(&quot;Any-name&quot;);&lt;/pre&gt;&lt;br&gt;&lt;br&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;br&gt;&lt;br&gt;If you get the error message &amp;quot;Unable to find the requested .Net Framework Data Provider.  It may not be installed.&amp;quot; then you will also need to add some extra lines to web.config.&lt;br&gt;&lt;br&gt;&lt;pre&gt;&amp;lt;configuration&amp;gt;&lt;br&gt;    &amp;lt;system.data&amp;gt;&lt;br&gt;        &amp;lt;DbProviderFactories&amp;gt;&lt;br&gt;            &amp;lt;add name=&quot;MySQL Data Provider&quot; invariant=&quot;MySql.Data.MySqlClient&quot; description=&quot;.Net Framework Data Provider for MySQL&quot; type=&quot;MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data&quot; /&amp;gt;&lt;br&gt;        &amp;lt;/DbProviderFactories&amp;gt;&lt;br&gt;    &amp;lt;/system.data&amp;gt;&lt;br&gt;&amp;lt;/configuration&amp;gt;&lt;/pre&gt;</description>
<comments>https://johna.compoutpost.com/blog/585/connecting-to-mysql-database-with-asp-net-web-pages-razor/#comments</comments>
<pubDate>2011-05-20T12:00:00+10:00</pubDate>
<category>ASP.NET Web Pages</category>
<guid>https://johna.compoutpost.com/blog/585</guid>
</item>
<item>
<title>Finally... A Replacement for Classic ASP</title>
<link>https://johna.compoutpost.com/blog/584/finally-a-replacement-for-classic-asp/</link>
<description>Recently I noticed that on the &lt;a href=http://www.asp.net/&gt;ASP.NET website&lt;/a&gt; there was a new menu option. In addition to &quot;Web Forms&quot; and &quot;MVC&quot;, there is now &quot;Web Pages&quot;. This leads to information on &lt;i&gt;ASP.NET Web Pages and the new Razor syntax&lt;/i&gt;.&lt;br&gt;&lt;br&gt;After a brief look through the documentation it appears to me that this is he closest we have to a replacement for Classic ASP!&lt;br&gt;&lt;br&gt;Although it goes against some of the coding best practices established by ASP.NET, such as separating content from code, to me it seems like an excellent way to get back to coding like I used to with Classic ASP -- simple, efficient code with the programmer in control instead of Web Forms.&lt;br&gt;&lt;br&gt;I have read some opinions on &quot;Web Pages&quot; and I disagree with those who claim that this new development option will only appeal to those looking for a simple way of producing dynamic websites. It appeals to me as I like to have more control over the HTML sent to the client browser and minimise the amount of server traffic (no viewstate data), and am sick of creating repeaters with OnItemDataBound events to do things that were so much simpler in Classic ASP. Surely the simple option is &quot;Web Forms&quot; with drag and drop controls and code-less development.&lt;br&gt;&lt;br&gt;Microsoft has made available a free tool called WebMatrix as a simple tool for creating web sites, but Visual Web Developer or Visual Studio 2010 is the better choice for professionals. You will need to download the MVC 3 Tools from Microsoft.&lt;br&gt;&lt;br&gt;I plan to produce some test code and see how well it works out. I see it combining well with JQuery for doing AJAX.&lt;br&gt;&lt;br&gt;The official website is at &lt;a href=http://www.asp.net/web-pages&gt;http://www.asp.net/web-pages&lt;/a&gt;.&lt;br&gt;&lt;br&gt;There is an excellent 270+ page PDF book available from Microsoft at &lt;a href=http://download.microsoft.com/download/C/E/A/CEA20EA5-5AEA-494D-B9D1-B082366FCA38/ASPNETWebPagesWithRazorSyntax-Book.pdf&gt;ASPNETWebPagesWithRazorSyntax-Book.pdf&lt;/a&gt; (free download).</description>
<comments>https://johna.compoutpost.com/blog/584/finally-a-replacement-for-classic-asp/#comments</comments>
<pubDate>2011-05-18T12:00:00+10:00</pubDate>
<category>ASP.NET Web Pages</category>
<category>ASP.NET Web Forms</category>
<guid>https://johna.compoutpost.com/blog/584</guid>
</item>
</channel>
</rss>
