aspx 2 razor code conversion
Can anyone convert the following code to Razor, i tried telerik aspx 2tazor, but开发者_StackOverflow社区 thats not working. when I use captcha with aspx its working, but i dont know how to convert the below code to make it work for razor.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.CaptchaData>" %>
<fieldset>
<legend>Please provide your name and a comment</legend>
<p>
<%: Html.XCaptcha().Image(m => m.EncrypedSolution) %>
</p>
<p>
<label for="Attempt">
Characters above</label>
<%=Html.TextBoxFor(cd => cd.Attempt)%><%=Html.ValidationMessageFor(cd => cd.Attempt)%>
</p>
<%: Html.AntiForgeryToken() %>
<input type="submit" name="submit" id="submit" value="submit" />
<%: Html.ValidationSummary() %>
</fieldset>
</div>
<script src="../../Scripts/xcaptcha-2.0.js" type="text/javascript"></script>
</body> </html>
Have you tried http://razorconverter.codeplex.com/?
I too have been investigating how to convert an existing site with aspx to have Razor views and this tool has helped a lot. It isn't perfect, and it doesn't get you 100% of the way there but it does help you save time converting files.
The approach I found helped me in getting the most out of this tool was to create two asp.net mvc websites...one with ASPX and the other with Razor...then I used the tool to convert the aspx site...and then compared the results with the Razor site which helped me spot what additional steps I needed to follow in order to fully switch over to Razor. I plan to use that learning experience to convert an existing website...
One disadvantage I found with this tool (and maybe there is a way to avoid this that I am not aware of) is that it creates all your cshtml files as lowercase files rather than keeping the camel case...
Another disadvantage is that it seems to leave @(...) around lots of lines of code...but they are easily removed.
One nice thing is that this tool also converts your Site.Mastor which you will then have to rename to _Layout.cshtml
The result for the razor converter for your code is:
@model MvcApplication1.Models.CaptchaData
<fieldset>
<legend>Please provide your name and a comment</legend>
<p>
@(Html.XCaptcha().Image(m => m.EncrypedSolution))
</p>
<p>
<label for="Attempt">
Characters above</label>
@(Html.TextBoxFor(cd => cd.Attempt))@(Html.ValidationMessageFor(cd => cd.Attempt))
</p>
@(Html.AntiForgeryToken())
<input type="submit" name="submit" id="submit" value="submit" />
@(Html.ValidationSummary())
</fieldset>
</div>
<script src="../../Scripts/xcaptcha-2.0.js" type="text/javascript"></script>
</body> </html>
Another tool that I have found useful is WebFormsRazorConverterVsExtension
It utilizes Telerik's razor-converter under the hood...and although you said that wasn't working out for you, when I used it to convert your sample, it worked out great for me...so maybe you should give it a try.
It doesn't convert Site.Master...so you may need to use a combination of both of these tools to get your site converted...but the output is cleaner than razorconverter above...
Here is the result I get when I use the WebFormsRazorConverterVsExtension:
@model MvcApplication1.Models.CaptchaData
<fieldset>
<legend>Please provide your name and a comment</legend>
<p>
@Html.XCaptcha().Image(m => m.EncrypedSolution)
</p>
<p>
<label for="Attempt">
Characters above</label>
@Html.TextBoxFor(cd => cd.Attempt)@Html.ValidationMessageFor(cd => cd.Attempt)
</p>
@Html.AntiForgeryToken()
<input type="submit" name="submit" id="submit" value="submit" />
@Html.ValidationSummary()
</fieldset>
</div>
<script src="../../Scripts/xcaptcha-2.0.js" type="text/javascript"></script>
</body> </html>
Also, I found the following references helpful and so they may help you doing the conversion.
- http://www.beletsky.net/2011/07/switching-from-aspx-to-razor-view.html
- http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
精彩评论