Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Saturday, February 04, 2012

How to create a login page using ASP.NET MVC 3 Razor

I am back to MVC project. I used MVC for web application long time ago when it was still in Microsoft Patterns & Practice development. Now it is part of .Net 3.5 and 4.0 framework and all the classes and templates are available in Visual Studio 2011.

I need to add a Login page to my project. The way I did in my ASP.Net is quite different from the way in MVC. Since MVC has been changed a lot in terms of structure and framework, I have to learn MVC again. The MVC templates created by Visual Studio 2010 do not have Login feature. Fortunately, I found this tutorial on Youtube in short time:

The tutorial example is what I want.  It seems very easy to add Login page to MVC project.

First, add restriction in web.config file with Location element:

<configuration>
   <location path="">
      <system.web>
         <authorization>
            <deny users="?"/>
         </authorization>
      </system.web>
   </location>
</configuration>

The above will disable access to all the pages in the site.  Then add authentication element for login page in web.config:

<authentication mode="Forms">
  <forms loginUrl="~/Login/Login.aspx" timeout="120" />
</authentication>

In order to ensure the authentication in other web pages, a cooky is added before redirect to other forms on server side, as an indication been authenticated (part 3):

FormsAuthendication.SetAuthCookies(key, persistent);

In the place where log out is implemented, the following code is used to clear the cookie (part 4):

FormsAuthendication.SignOut();

That's all the basics.

In addition to those steps, the tutorial explains master page in MVC, which is in the Views->Shared folder. Different master pages can be specified (part 4). One interesting thing in MVC is that cs and html are all in one source code file as cshtml extension.

The tutorial also explains some very basic concepts in MVC:
  • Router structure in Global.assax (part 1)
  • Model for a view (part 5)
  • Get and Post actions mapping to View's methods

Read More...

Friday, November 11, 2011

Working on an ASP.NET Project

Last week I started working on an ASP.NET project.  At the start, I was planning to use ASP.NET MVC 3.  I worked on MVC project 5 years ago.  That project was based on initial framework by Microsoft Patterns and Design group. Now this MVC has been changed a lots. It seems much easier to work with.

The first thing was a spike on this new framework. I tried one example from ASP.NET MVC web page.  One thing I don't like is that the project is integrated with Microsoft Entity Framework and SQL Express.  I had to break the connection to the SQL db to make it work. It took me one day to get main web page working.

Then I deployed to  our dev IIS, which is in a box of Windows Server 2008 R2 with .NET 2.0, 3.5 available. Unfortunately, the initial deployment was not working.  The project I created was based on MVC 4 and .NET framework 4.0.

I installed .NET framework 4.0. Still the web page on IIS was not working. I installed ASP.NET MVC package on the box. Still not working.  It looks like the Razor engine was not configured on IIS. I got 404 error, denying access to the content of a path, which is the basic structure of {site}/{controller}/{action}/, with no .aspx. Finally, I got it working by installing WebMatrix.

With all those installation, my MVC example web page was displayed on the IIS. I was very happy to tell my project BA, who is the main force to push this project with new framework. Unfortunately, on the second day, I realized that all the existing web pages stopped working. That's really bad news.

Since my project is in a very tight schedule, I made decision to roll my project back to a .NET 2.0 based ASP.NET web project. This is the one consistent with our existing web pages. I blamed Microsoft for IIS not being able to support .NET 2.0 and plus web pages.

Two days later, I spent several hours on IIS configuration. I saw that all the web sites on the deb box was based on application pool by defaults, which is .NET 4.0.  In the screen of application pool, there are previous ones based on .NET. After I changed the AP for existing web sites to .NET 2.0, all those sites are back to work. It is the updates that causes the AP changed. Still I think the installation should not change those AP settings.

That's quite up and down experience. I have learned good lesson about IIS configurations. IIS does support parallel .NET frameworks by specifying AP. Just imaging IIS as an application, and all the web sites are web pages based on all libraries.  The IIS is a complicated application. With IIS management console, the configuration can be set for web sites, which supports previous ASP.NET projects.

I'll continue to work on this project based on .NET framework 2.0 and ASP.NET. The next phase is to update it to .NET 4.0, hopefully with MVC 3.

References

Read More...

Saturday, October 11, 2008

Web Tool: Short URL

I read an article about web tool to make URL short long time ago (last year?). That is ShortURL. However, the site may not free soon as I recall a warning in the article. Now I think that I do need a tool to redirect long urls such as my blog entries to a short one. By gooling web, I found MetaMark.

The short URL generated by this site is not a user friendly one, but it works. According to the information of this site, if the url has not been used for more than two years, it may expire. For short term use, this web tool is great. For example, my previous post is at http://xrl.us/otgzf, comparing to http://davidchuprogramming.blogspot.com/2008/10/web-tools-firebug-and-smushit.html.

Here is the form I copied from the site to do the job:



Enter a long URL to make shorter





I did several tests. If you use it to generate one and try the same long URL again, the one first time generated is returned back. Therefore, you just cannot change it. It does not check if the URL exists or not. I gave a none-existing one, and it still generated a new short name, which is not not-found-url. May be this is a way to change it (by making the previous existing one invalid). The web server just generates an alternative short URL, no matter the URL you provide is valid or none-short one. I tried to get a short URL for "http://google.ca". The new one is "http://xrl.us/bemw2" (it may exist or I just generate one for it):).

A related technique about URL is to implement ASP.Net's MVC framework. Its URL is based on controller, action and router. You can construct your own user-friendly URLs. The URLs based on ASP.Net MVC does not contain .aspx or .html suffix and not url parameters like ¶1=value1... You will see more short and user friendly URLs on web. The Metamark page mentioned that it is using MVC pattern. Maybe this server just use the pattern to build a very simple, random and short URL mapped to a long URL.

You can do the same thing for your company's web pages. Most of them are not-user-friendly URLs and hard to remember. By using ASP.Net and MVC framework, you can define a structure of URL and mapping user-friendly URLs to a existing ones without any change to the old ones. It should be very simple project.

Update: One of the great feature of Metamark web tool server is that you can provide a nickname and secret. The nickname must be unique one, not taken by others. Then the generated URL is [server_home]\[nickname]-[secret]. You could create a user-friendly URL! This blog is at http://xrl.us/shortURL1-WebTool. "shortURL" has been taken. I had to add "1" to it.

The generated short URL does not take any suffix like ".aspx" or ".html", which is not needed actually!

Read More...

Thursday, October 02, 2008

ASP.Net MVC

Just watched a training video Creating Task List on ASP.Net MVC. It is very impressive. MVC design pattern has been used for Windows application and now the framework for ASP.Net is available.

I have used this or MVP in several desktop applications before and I have not touched this type of design for several month. I'm very glad to see the new package available.

MVC pattern provides very good design infrastructure with separations of UI, Controller and Model. In addition to this model, I prefer to add a Repository level to separate data base and classes. The Repository model is responsible to provide data as objects and interface for updating objects back to DB.

Any good design pattern does not resolve all the issues. Still you have to keep good practice to use them. I understand that the tutorial video is just an instruction to MVC. My comments on the practice in this video is that there are too many client to server calls. To create a new task you have to make a call to server side to add a new task. However, to mark a task as completed, I don't think that the Complete action call to server for each edit is necessary. It could be done on client side to mark one or more tasks as completed. Add a Save button to save changes. This will avoid frequent calls to server.

In order to do that, I think client side script may be needed to handle these changes. In addition to that, you have to handle redirect change by client to another page. That means a warning message to indicate changes.

New design patterns would provide a gateway to design a better applications. You should remember that that does not resolve all the problems or make your application in a good structure. It'll take time to master skills to develop good applications.

Read More...