p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 (http://p2p.wrox.com/forumdisplay.php?f=472)
-   -   Chapter 5 UserController.cs P.119-129 (http://p2p.wrox.com/showthread.php?t=75635)

ralphbethke August 10th, 2009 10:53 AM

Chapter 5 UserController.cs P.119-129
 
I am following your book very closely except I have renamed the project MvcBeerHouse. Things went fine until I added the UserController.cs and then my Error List went wild. Where are the following classes for [Service, HttpPostOnly], TheBeerHouse.controllers.ServiceAttribute, ManagedFusion.Web.Mvc.HttpPostOnlyAttribute and namespaces defined? What's ManagedFusion.Web.Mvc?

Error 1 The type or namespace name 'ServiceAttribute' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 342 10 MvcBeerHouse
Error 2 The type or namespace name 'Service' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 342 10 MvcBeerHouse
Error 3 The type or namespace name 'HttpPostOnlyAttribute' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 342 19 MvcBeerHouse
Error 4 The type or namespace name 'HttpPostOnly' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 342 19 MvcBeerHouse
Error 5 The type or namespace name 'ServiceAttribute' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 373 10 MvcBeerHouse
Error 6 The type or namespace name 'Service' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 373 10 MvcBeerHouse
Error 7 The type or namespace name 'HttpPostOnlyAttribute' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 373 19 MvcBeerHouse
Error 8 The type or namespace name 'HttpPostOnly' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 373 19 MvcBeerHouse
Error 9 No overload for method 'Redirect' takes '2' arguments C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 105 40 MvcBeerHouse
Error 10 No overload for method 'Redirect' takes '2' arguments C:\Projects\MvcBeerHouse\Controllers\UserControlle r.cs 107 40 MvcBeerHouse

fjp August 10th, 2009 01:03 PM

Hi. I am trying to create a new MVC Project too and I get the error #9 and #10 everytime I use the 'Redirect' method. As far as I see, the definition of 'Redirect' method in my new project is inside System.Web.Mvc.Controller.cs [from metadata], but the same method has its definition (as extension) in System.Web.Mvc.ControllerExtensions.cs [from metadata] in the original project. Did I miss something? Any help is welcome.

nberardi August 10th, 2009 01:06 PM

Please make sure to download the latest code from CodePlex.

http://thebeerhouse.codeplex.com

It contains the necessary assembly to get that code working.

ralphbethke August 10th, 2009 03:19 PM

Chapter 5 UserController.cs P.119-129
 
Hey Nick

Thanks for the quick response.
I have the latest download and there's no issue with that. The problem is with neophytes like me. I am using your book as a learning textbook and there are a lot of implied steps I'm having to research. For example, the Resource file ManagedFusion.Web.dll needs to be added to the project to get rid of the errors I listed earlier in my thread. I'm starting this project from scratch...not reverse engineering TheBeerHouse, but I'm ending up having to work backwards just to see how things are done. Otherwise, I could have just downloaded the code without buying the book.

Where can I find more info on ManagedFusion.Web.dll?

nberardi August 10th, 2009 03:46 PM

That is totally understood, but think of the ManagedFusion.Web.dll as a resource, much like the style sheets, images, or anything else that are not easily transferred through a book.

At the time of writing the book, there were many things that MVC was lacking that are necessary to have a good website. Mostly because it is still in it's first release, and the MVC team gave us the basic framework, but not necessarily all the tools to complete the job.

While we were writing the book, we didn't know what the MVC team was going to introduce and what they were going to hold off for a future release. So it was a tricky process writing the book and keeping up with the new MVC releases. We ended up stripping out about 75% of the original functionality of ManagedFusion.Web.dll because it was deprecated in favor of the MVC code that did a similar functions.

Also there was a conscious decision to help grow the readers knowledge, especially for beginners. If we jumped right in to creating ActionFilters and specialized ActionResults for handling JSON content and other things, before we introduced basic concepts and the constructs of a proper controller, we would alienate many of the readers because the flow of the text would be confusing.

We hope to eventually fully remove ManagedFusion.Web.dll from the project, in favor of some more chapters or when MVC fully deprecates all the features it provides. That is if there is an MVC 2.0 Website Programming book.

Wish you the best,
Nick

ralphbethke August 10th, 2009 04:04 PM

Chapter 5 UserController.cs P.119-129
 
Hey Nick

I fully understand. As I go through your book and Pro ASP.NET 1.0 I'm not sure how I would organize the material. I think you guys did a great job and...don't get me wrong, I never regret buying your book because of the wealth of other info I get from it. And I think that's part of the problem. There's so much info that's changing so fast I can hardly keep up. Just look at the number of books WROX is publishing this year.

I love your book and am looking forward to your next venture.

Thanks for your help,
Ralph

Al_Katawazi August 11th, 2009 09:51 AM

I was curious so I removed the managed fusion DLL just to see what would happen. I have been using the user control for a lot of other projects and did not need the managed fusion dll and here is what I was able to piece together for you.

The User Controller has about 3 or 4 errors pertaining to the service httpPostOnly attribute flag. If you replace that with this:

[AcceptVerbs(HttpVerbs.Post)]

As for the other errors they pertain to the redirect method. You can easily replace that with a Redirect or RedirectToAction method to get the result you are looking for. In terms of the rest of the project, I think most of the errors are similar.

nberardi August 11th, 2009 10:07 AM

Quote:

Originally Posted by Al_Katawazi (Post 245102)
The User Controller has about 3 or 4 errors pertaining to the service httpPostOnly attribute flag. If you replace that with this:

[AcceptVerbs(HttpVerbs.Post)]

Actually Al is not correct about this, HttpPostOnly and AcceptVerbs(HttpVerbs.Post) serve two different functionalities, and it is wrong to equate the two.

AcceptVerbs is what is known as an action method selector, it inherits from the ActionMethodSelectorAttribute and is used as a pre-processor when the route is looking for the correct action method to use in conjunction with the current URL being requested. In the case of AcceptVerbs it compares the HTTP Verb (GET, POST, OPTIONS, DELETE, etc) to the verb that is being requested from Request.HttpMethod.

http://aspnet.codeplex.com/SourceCon...w/23011#266431

There is a problem with this in that if you don't have another action method with the same name, then the route will just skip by this. And might pick up another action method farther down the line. This can cause serious problems in your application, in that the chosen action method is now out of your hands, and it might happen on an action method that it wasn't suppose to and expose some serious problems. So please when ever you implement the AcceptVerbs attribute, make sure you at least account for GET and POST, if not all the other HTTP Verbs. Because if this is not thoroughly tested with all of them you could be opening up your application for a world of hurt.

See my Issue I posted to the ASP.NET team specifically about this issue.

However HttpPostOnly works differently in that it doesn't interrupt the route from finding the controller, it just throws the proper HTTP error response when the wrong Verb is used with the action method. Which is 405 Method Not Allowed and then responds with the methods allowed.

Here is the core of the HttpPostOnly.
http://code.google.com/p/coderjourna...lowedResult.cs

Quote:

Originally Posted by Al_Katawazi (Post 245102)
As for the other errors they pertain to the redirect method. You can easily replace that with a Redirect or RedirectToAction method to get the result you are looking for. In terms of the rest of the project, I think most of the errors are similar.

This is another place MVC was lacking and if you want proper handling of URL's you must keep the functionality of ManagedFusion.Web.dll. You can't simple replace it with Redirect or RedirectToAction, because they use the 302 Temporary Redirect, and what you actually want is a 301 Permident redirect. If you use a 302 redirect search bots don't update their URL to the new URL, and they keep the old one and the new one indexed in their system. However with a 301 redirect a search bot knows you want to move the URL, and they only keep the new one indexed.

This is important so you don't suffer from duplicate content penalties.

Al_Katawazi August 11th, 2009 10:41 AM

I'm sorry Nick I haven't delved that much into what exactly is happening with the ManagedFussion.dll, I assumed the functionality was similar to the MVC Framework attribute flags and methods.

In reference to ralphs situation though, he can use either or. True you will not get the most elegant responses or functionality with the MVC Framework methods but they are part of the framework unless you propose he include the a managed fusion dll in all his new projects. Not that I am saying there is anything wrong with that since the source is out there but if ralph wants to stay within the framework those are some good alternatives.

nberardi August 11th, 2009 11:16 AM

Quote:

Originally Posted by Al_Katawazi (Post 245107)
I'm sorry Nick I haven't delved that much into what exactly is happening with the ManagedFussion.dll, I assumed the functionality was similar to the MVC Framework attribute flags and methods.

In reference to ralphs situation though, he can use either or. True you will not get the most elegant responses or functionality with the MVC Framework methods but they are part of the framework unless you propose he include the a managed fusion dll in all his new projects. Not that I am saying there is anything wrong with that since the source is out there but if ralph wants to stay within the framework those are some good alternatives.

If you really want to move away from ManagedFusion.Web.dll, copy the files to your local project. (it can be found here)

Al is wrong in equating the two because, AcceptVerb is a URL processor helper, and AcceptVerb should be used in these cases:

[AcceptVerb("GET")] public ActionResult GetList (int id) { ... }
[AcceptVerb("POST"] public ActionResult UpdateList(int id, string[] list) { ... }

And HttpPostOnly or anything in the AllowedHttpMethodAtribute class are security related functions. Equating the two is like saying URL Rewriting is like Forms Authentication, they are not even in the same ball park. And it is critical that you don't use AcceptVerb as a security measure, which is why I created the HttpPostOnly attribute.

ralphbethke August 11th, 2009 11:19 AM

Chapter 5 UserController.cs P.119-129
 
Hey Guys

You the Men!
It's great to know that all that good Beer was not wasted on you.

A preview release for MVC2.0 is now available so the dll issue might be a moot point. I'll check it out.

Thanks again for all your help
Ralph

nberardi August 11th, 2009 11:33 AM

Quote:

Originally Posted by ralphbethke (Post 245116)
You the Men!
It's great to know that all that good Beer was not wasted on you.

A preview release for MVC2.0 is now available so the dll issue might be a moot point. I'll check it out.

Some of the issues have been resolved, however some haven't. Plus a good lively debate never hurt anybody. [B)][:)]

The MVC team really did a disservice by making the AcceptVerb attribute look like a security mechanism. Because it was never intended to be that, and it has fooled many developers. A proper security system rejects the attempt right away, which is what HttpPostOnly was designed to do. It is like saying "immediately reject if the POST verb doesn't match". However AcceptVerb is sort of like a gateway, which says "skip me if you don't have a POST verb". The "skip me" part is where you can have many problems if you have a lot of route rules, or if your application will some day have a lot of route rules, because after this action as been skipped, it will go on and find all the other actions that may match. If it finds one that it wasn't intended to match to, you have a hole in your application that is very hard to find.

That is why I am so passionate about this particular attribute, because it has the ability to give people a false sense of security. Because I fought this tooth and nail over in CodePlex, for them to add more security to this particular action method filter, because me and some others saw the chance for abuse.

http://aspnet.codeplex.com/WorkItem/...orkItemId=2201

I am really glad you like the book, and look forward to seeing your review on Amazon.

kkhan October 26th, 2009 01:12 PM

Can't nuild this solution
 
I recently bought the book and it looks very promising but I got in same troubles when diving into the actual code.

As per suggested, I tried to subscribe to SVN code branch at codeplex but I get when sync reaches as at \...\Source\TheBeerHouse_35_CS\BeerHouse35\App_The mes\DarkBeer\images\.svn'.


Now fortunately, I was able to download actual zip archive from codeplex but it has the the same famous errors with redirect methid and [ServiceOnly, HttpPostOnly] attributes. Is there any link to get the missing ManagedFusion.Web.dll? further to my surprise, there are no broken references appearing. Also how to get around that redirect overload problem. Thanks

nberardi October 26th, 2009 01:29 PM

Check in the /Resources directory of the downloaded zip file. Everything is there that you need.

fourpastmidnight May 27th, 2010 09:15 AM

I'm disappointed with this book
 
I'm very disappointed with this book. I came to this forum searching Google for where the ServiceAttribute and HttpPostOnlyAttribute classes were in the .NET Framework, only to find out they don't exist, but are in this ManagedFusion.dll.

Now, I'm not against having a separate library, but not once did the book mention needing to make a reference to this assembly in the project. Like another poster commented, I'm using the book as a learning guide/tutorial. I expected from the title, "Problem, Design, Solution", that this was a step-by-step guide (not necessarily for beginners, but a step-by-step guide nonetheless). To make people have to try to guess how you "designed" this application is poor, given the title of this book. In this regard, I thought the original ASP.Net 2.0 book by Marco for this website was better.

Also, why did you completely re-engineer the membership section of the site? The default MVC 1.0 application template provided a fairly complete account management controller. It even validated certain fields during membership registration which your incarnation of the wheel did not even take into account (would that make it a square wheel?), possibly making the site much less secure. If there's functionality already written, use it, unless it's really really bad. There's nothing worse than re-creating the wheel. Keep it DRY.

I also fault the book with not using very good semantic markup and too much use of inline styles (esepecially in the ManageUser.aspx page). Also, the editor on this book did not do a good job.

I appreciate the fact that you guys even went out to write this book. I appreciate what you're trying to get across. But these issues I've listed above really take away a lot of the good you were trying to get across.

Don't take this too presonally, as I've seen this in Wrox books a lot over the past 5 years. The quality of books going to print (and not just Wrox, I might add) has really gone down and it's a real shame. If you're going to set out to teach, do it well and do it right. Otherwise, don't waste your time.

Another really positive note (as opposed to my mostly negative ones [:(]) : I really liked the discussion in this thread over the use of AcceptVerb vs. HttpPostOnly. I think that the author is correct in his assertions. I was under the assumption that AcceptVerb was a security measure. Upon closer inspection, this was an incorrect view. I was shocked and glad I haven't yet developed a site with MVC, as I would have inappropriately used that attribute for security when it really is a hint to the routing engine and that's it.

I sure hope the next edition of this book will have better use of semantic markup and will clearly explain all the steps needed to design a solution to the problem discussed, as the title implies. I also hope that the use of the DRY principle will also be increased. I hope that next time, I'll be commenting on how good the next edition of this book is.

Sincere Regards,

fourpastmidnight

nberardi May 27th, 2010 11:08 AM

This is a common question and...

A little context is probably necessary to address most of your concerns. This book was started with PR2 of the MVC Framework, and most of what you see today in the MVC 1.0 Framework wasn't yet in the framework at the time. We as the authors did out best to keep the book as close to the framework as possible with each of the 7 releases after PR2.

There were many challenges including keeping the API up to date and other things that come along with trying to write a program for an actively developing framework. Which included, API Changes, Name Changes, Style Changes, Syntax Changes, and full examples like the Membership provider.

This is why you see the "quality" of books going down as you put it. I don't necessarily agree or disagree with that statement. Because these technical books now have to compete with freely published blogs with mostly the same content. And these books take a year or more to complete, so inorder to release the book at the same time as a new framework, many things have to be done in parallel. And unlink blogs we cannot just ignore what we wrote in the past, we have to go through and update the book, which is very time consuming.

So we have to balance our book schedule release, with other logistical factors around the printing press and release dates and framework schedules.

This is why you don't see an exact copy of the membership provider, because it was created way back in PR2 when we first started coding the project, and the current example didn't exist in the MVC solution. We did our best to bring it as close as possible as we could with out having to rewrite the entire chapter from scratch.

I hope this helps in understanding logistically and gives you a context of what it takes to create a book that comes out at the same time as the framework while still trying to stay relevant with a changing framework, even when starting the book a good year before the framework was officially released.

fourpastmidnight May 27th, 2010 12:30 PM

Thanks for the clarification
 
Nick:

I again want to stress my thanks for at least gathering all this material and publishing this book. My aim is not to disparage you and your co-authors, nor discourage you from publishing. In fact, to the contrary--I hope that it will spur you on to continue writing and improving the craft (and I'm not saying you're a bad writer, but as you write more, you get better and better at it, it doesn't matter how many years you've been writing [;)]), as well as the content of the book.

I know the beginnings of the MVC were very immature. I did not realize, however, that you started with that early of a release! That does put quite a bit more perspective on the book. One thing that could've helped alleviate some of these concerns is by stating in the Introduction to the book what release of the framework you were writing the book against--since, as you know, the framework was changing almost daily--there were bound to be changes from the time of publication to the time of the release of MVC 1.0.

But again, I hope that you will take the things I said to heart, such as using better semantic markup (so that your content is SEO friendly, as well as your URLs), better formatting of example code in the book, and better description of what should be done when and when 3rd party libraries are in use and when/where to reference them.

I understand the fierce competition with blogs. As much as I like finding information on the web, sometimes it's good to just sit down with a book and leaf through the pages. So, for me, it's best to have a well written, well edited book. Books will never beat blogs in terms of their content, clarity, and cohesiveness (well, the way a lot of books have been done in the past few years, dare I say that perhaps books are "falling down to the level" of blogs--but this is not against the authors, but rather all publishers).

I sure hope that the book industry at large, and their authors will continue to produce high-quality material that is irreplaceable (even from a competition stand point). I look forward to another edition of this book, if there is one forthcoming.

Perhaps an errata item could be made regarding the use of 3rd party libraries, their MVC 1.0 RTM equivalents (if available). Also, in chapter 5, the code listing forgot to include the Login action method (however, I got it from the download, but I was confused when I went to make the Login View knowing I hadn't written an action for it in the UserController).

Again, thank you for the clarification, and your hard work. Providing (I hope constructive) criticism helps make your work better in the future, which helps me get better, so it's in my own best interest, as well as yours, to provide it. If I offended with my comments, I did not intend to, and did not intend anything personal--I'm just worried about the quality of books I see coming out from publishers (and the authors whose names are attached).

Sincerest Regards,

fourpastmidnight

ulikkg May 27th, 2010 05:25 PM

So where is the solution
 
I am not agree with "fourpastmidnight" about not creating complete new Membership Managing interface and using ASP.NET's pages. It is always good to have own user management GUIs and sections specific for the project. For example for the branding of the website it is very important. Also it is good to have own implementation as it is not existing in MVC.

Anyway, for 2,3 days I am facing a problem for which I cannot get a solution neither in Wrox nor in codeplex. I finished solution part of Chapter 5 - Membership implementation. When I run the project I am having an errors in UserController.cs at role deletion part related to [Service, HttpPostOnly]. Here:

[Authorize(Roles = "Admin")]
[Service, HttpPostOnly]
public ActionResult DeleteRole(string id)
{
Roles.DeleteRole(id);
return View(new { id = id });
}

As I researched it is related to external library called ManagedFusion something on which you discussed a lot in this post.

Nick, you have explained a lots of staff - I appreciate that but I did not see any concrete & exact step-by-step explained solution. As stated by other readers I bought this book to follow Problem-Desgin-SOLUTION steps but not to research blogs. Still being NOT completely new to ASP.NET, I now really stucked at this point. Please provide this missing explanation for your book. As an additional information I am using MVC 2.0 with VS 2008. I finished Solution part of Membership Management - chapter 5. Till that point I did not see anything about ManagedFusion, neither ServiceAttribute.cs if they are also needed please include in explanation.

I am not upset using external library. But explanation should be provided. Also, explanation can be forgotten - maybe. Then it must be covered in these forums or in companion websites which is given in the book.

Additional errors for which no explanation exist are

-Iso3166CountryCodes - does not exist in current context
-TheBeerHouseDataContext in GetOccupationList. Where does this datacontext is created and how? When did you create this occupation list table in DB
- return this.Redirect(303, userInformation.ReturnUrl); argument problems in user Registration action result.


PLEASE PROVIDE STEP-BY-STEP SOLUTION FOR ABOVE ERROR FOR FOLLOWERS OF THE BOOK. PLEASE!

nberardi May 27th, 2010 06:18 PM

Remember how I said most of what we had done and needed for a real site wasn't part of PR2 when we started the project? Well we created many of our own action filters, and slowly replaced them as they became available in the actual framework. Even coming up to the final months before we published the MVC team was still making major changes and adding new functionality and we didn't really know if these filters would get included in the framework or not. There was high hopes that they would, so we wouldn't have to rely on a third-party library. But it didn't workout that way.

However here is the latest copies of the libraries:

http://github.com/managedfusion/managedfusion
http://github.com/managedfusion/managedfusion-web

They are also available in the original published form on the CodePlex website which is way more up to date than the Wrox site.

http://thebeerhouse.codeplex.com/Sou...w/61392#759455

Hope this helps.

fourpastmidnight May 27th, 2010 06:24 PM

Referencing ManagedFusion.*dll in your Project
 
ulikkg:

First, I think you misunderstood what I meant by "customized" user membership. You are correct that you would want to design your own membership pages (as far as HTML and presentation are concerned). However, what I was talking about is that ASP.NET MVC provides all the code and logic for user membership and role management in the AccountController.cs file that's included in the standard MVC 1.0 Web Application project template.

As discussed by Nick and I, the book used a very early release of MVC. So the functionality I was talking about that comes "out of the box" came out after the book went to print in the final released version of ASP.NET MVC, hence why I asked about Nick "re-writing" all that code.

As for the ManagedFusion DLL, I'll give you a hand.

First, download the code from codeplex if you haven't already. When you open the zipped file, there will be a folder called Resources. Inside this folder are the ManagedFusion.dll, ManagedFusion.pdb, ManagedFusion.Web.dll and ManagedFusion.Web.pdb files. The *.pdb files, if I remember correctly, are for debugging purposes, so you probably want those, but they aren't strictly necessary.

I created a new directory in my project called 'lib' and extracted these four files into my newly created directory. (You can call the directory anything, but I called it 'lib'). You don't need the System.Web.Mvc.dll file because it's part of the standard ASP.NET MVC framework.

Once you have extracted the files into a folder in your project, you now need to reference them. Right-click the References folder in your project and choose Add Reference.... In Visual Studio 2010, there's a tab in the dialog box that opens called Browse. I don't know if there is a similar tab in Visual Studio 2008, but you need to browse to the directory where you placed the DLLs and choose both the ManagedFusion.dll and ManagedFusion.Web.dll files.

Lastly, in the UserController.cs file, add the following statement under your other 'using' statements:

Code:

using ManagedFusion.Web.Mvc;
That should be it for that.

HTH

ulikkg May 27th, 2010 06:40 PM

?
 
Hi fourpastmidnight,

Thanks a lot for explanation! With one word - GREAT! I will try to follow your steps - I hope it solves.

Nick, I understand your case and it is not easy to write book parallel to the release and as I already said I am not against using external libraries. The point is I don't see any explanation neither in the book nor in the forums for the usage of external libraries. Now, you again send me the link to the files. What should I do with these files - not clear. I hope fourpastmidnight's explanation will help me to get out of the problem.

Other question is in the ProfileInformation.cs there are GetCountryList, GetOccupationList and GetLanguageList methods. In them I have error with TheBeerHouseDataContext. When and how did you create this datacontext - I know with LINQ but how exactly? When did you create these tables in DB? I am not going to download and run the whole code - I AM FOLLOWING THE BOOK and SURPRISES. You must understand us as a readers. Can you please clarify these errors in ProfileInformation.cs?

Regards & thanks in advance,
Ulik

fourpastmidnight May 27th, 2010 07:16 PM

Adding the LINQ to SQL Classes file to the project
 
ulikkg:

I'm currently having a problem with this, too. Let me help you with this, as I solved half of it.

The author told us at the beginning of the book that he would be using Linq-To-SQL. But again, never were there any steps in the book that explained how to do this, nor when. Luckily, I've had exposure to it before.

First off, the database is included with the source code in the App_Data folder in the TheBeerHouse folder that houses all the code for this project.

In your project, if you don't already have an App_Data folder, right-click your project, hover over Add ASP.NET Folder and choose App_Data.

Extract the database from the zipped up code files and place it in the App_Data folder. Then add the database files to your project in the App_Data folder.

Next, right-click your project and choose Add. On the categories pane, Web will most likely be selected. Choose the Data category instead.

On the right side, choose LINQ to SQL Classes. Name the file TheBeerHouse.dbml. Now, I already did this in my project, so I'm trying to recall this from memory. But, a dialog should pop up to get information it needs for making a connection to the database. The first dialog needs to know what the file will be connected to. In your case, I believe it is a SQL Server Database File (again, I'm stating these steps from memory).

I believe at that point, you will be asked to choose a file. Navigate to your App_Data directory and choose the *.mdb file. That should take care of getting the TheBeerHouseDataContext class. However, I don't think this will solve your problem.

I know for me, I need to create the Languages table in the database that was described in the Design section, but not the Solutions section. Also, apparently there's supposed to be an Occupations table that was never discussed in the book. [:(]

Let me know if you figure out anything. [:)]

fourpastmidnight May 27th, 2010 07:38 PM

Getting Chapter 5 to Work
 
Ok, here's the rest on the LINQ to SQL.

In Visual Studio, go to the View menu and choose Server Explorer.

Expand Data Connections. You should see your database there. In any event, expand your database and then the Tables folder.

Double-click your TheBeerHouse.dbml file to open up the LINQ to SQL designer. Now, back in Server Explorer, drag the Language(s) table and the Occupation(s) table (I'm not sure if they're plural or not) onto the designer surface. The designer will name the corresponding objects Language and Occupation. Rename these objects to Languages and Occuptations, respectively.

You should be good to go.

HTH.

ulikkg May 28th, 2010 04:47 AM

Thanks
 
Hi fourpastmidnight,

Thank you very much. I really appreciate your help and great effort. Currently I am at work I will try evening at home. Yesterday I went to bed :)

Best Regards,
Ulik

fourpastmidnight May 28th, 2010 05:43 PM

Getting Chapter 5 to Work
 
Well, I went on to chapter 6 after I figured out how to get Chapter 5 to work, only to find that in chapter 6 do they finally tell you how to set up all the LINQ to SQL stuff.

The editing on this book is terrible. How can you say in Chapter 5, "Well, that's everything you need to get the site working up to this point" when in reality, you need stuff in Chapter 6 to make stuff in Chapter 5 work??

Really, the organization of the content is about the worst I've seen (not _the_ worst, but right there with _the_ worst). This is not a reflection on the author, though he/they have some control over this--but rather, the editor and others involved in pre-production are the ones responsible for making sure that the content is organized properly before the book goes to print.

So if you can't get chapter 5 to work because you're missing classes, see chapter 6. In addition, as I stated before, the classes you will be missing are the Languages class and the Occupations class, which are LINQ to SQL classes from the Languages and Occupations tables in the database. However, chapter 6 does not tell you how to add these to LINQ to SQL, but it's easy enough to do. Just follow the last few steps of my previous post on this thread after having read through chapter 6.

ulikkg May 30th, 2010 08:16 AM

Lots of errors
 
Hi fourpastmidnight,

Finally I got it run. BUT except these errors there is lot's of missing parts. No mention about View inheritances, Some important actions are not existing/mentioned in book. Role and User deletion is not working still. Everytime I found solutions by comparing book with downloaded files and there are lots of differences. I am absolutely agree with you that they implemented LINQ to SQL from chapter 6 but they are being used in chapter 5 - what a silly thing.

As for database, in addition to other tables I put IsoCodes inside one table - why it is being retrieved from CSV file - not good practice for me.

Last comment about your last statement about role management. In standard MVC solution there is no role management interface - as I know there are only for Sign In, Registration, Forgot Password.

Thanks once again, I hope next chapters are more clear.

fourpastmidnight May 30th, 2010 05:20 PM

It took a lot to fix all the errors
 
ulikkg:

Well, first, I'd like to say that so far, I've managed to completely do away with the ManagedFusion.dll and ManagedFusion.Web.dll.

MVC 2.0 includes new attributes that can be used in place of the ManagedFusion.*.dll. For instance, whenever the HttpPostOnly attribute is used, you can replace it with the MVC 2.0 HttpPost attribute, which does the same thing. Apparently, according to the documentation for it, it's a shorthand for the AcceptVerbsAttribute. Any action decorated with the HttpPostAttribute will only accept requests with the HTTP POST verb. Perhaps the behavior of this has changed since MVC PR, which is what this book was based on (PR = Preview Release, essentially, an alpha version of the 1.0 framework).

Secondly, for the Service and Service only attributes, instead of returning a View( <some data> ), you can instead return a more specfic type of ActionResult, such as Json( <my json data> ). Then you don't need the ServiceAttribute or ServiceOnlyAttribute.

The IsoCodes are located in the source code in the folder App_GlobalResources. This took me a little while to get working. When you import all the files into your project, the project doesn't automatically "hook up" the *.resx code-behind files. I had to manually edit my project file to get everything to work.

Basically, right-click your project and choose Add --> Add ASP.NET Folder and choose App_GlobalResources. Then extract the files from the source code download in the same folder into your project folder. Next, close all files you have open associated with the project. Next, right-click your project and choose Unload Project.

Next, right-click the project and, IIRC, there is' an Edit Project item. In any event, it will open up the XML file that resembles your project. Locate the following XML:

Code:

<Content Include="App_GlobalResources\Messages.designer.cs" />
<Content Include="App_GlobalResources\Messages.it-IT.designer.cs" />

Replace it with the following:

Code:

<Content Include="App_GlobalResources\Messages.designer.cs">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>Messages.resx</DependentUpon>
</Content>
<Content Include="App_GlobalResources\Messages.it-IT.designer.cs">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>Messages.it-IT.resx</DependentUpon>
</Content>

This will hook up the RESX files with ther CSharp code-behind files. That should take care of your problem with the country codes.

You are correct about there being no Role management in the standard interface. In fact, I was mostly talking about the actual process of registering a user. Yes, in fact, you must create all your own administrative pages for (after-the-fact) user and role management, unless you use the default ASP.NET configuration website, which again, is not branded. But, you should more or less copy the code from the default management website provided by ASP.NET on the back end to take care of handling all the business/security logic, unless your security/business requirements differ from the defaults provided by MS.

Anyway, I hope you are having success, otherwise. I ran into a bump in chapter 6 where the author checks for checkbox values in the ArticleController action EditArticle:

Code:

if (IsPostBack) {
  approved = approved ?? false;
  listed = listed ?? false;
  commentsEnabled = commentsEnabled ?? false;
  onlyForMembers = onlyForMembers ?? false;
}

That's a big no-no. First of all, controller's have no idea about whether a page is a postback, therefore, this property doesn't exist inside controllers. The author's explanation is that on POST, checkboxes that aren't checked do not provide any value. The workaround is to make changes to both the view and the controller.

In the CreateArticle.aspx view, change the following;

Code:

<h3>Acticle Options</h3>
<!-- Got rid of the P tag, invalid XHTML having a UL nested inside a P -->
<ul class="field input options">
  <!-- Note, in MVC 2.0, the tag <%: by default HTML encodes the output
      so you don't need to call Html.Encode yourself. You should always
      use this in place of <%=. Again, this is MVC 2.0 only!
  -->
  <li>
    <%: Html.CheckBox("approved", new { value = "true" }) %>
    <label for="approved">Approved to be published</label>
  </li>
  <!-- Notice, I used an overloaded Html.CheckBox constructor which takes
      an anonymous object representing HTML element attributes.
      This will make sure that check boxes return the value "true" when
      they are checked, otherwise, they will return nothing.

      Repeat this for the other 3 checkboxes.
  -->
</ul>

Now, in your EditArticle action in your ArticleController, remove the "if" block so that it looks like the following:

Code:

approved = approved ?? false;
listed = listed ?? false;
commentsEnabled = commentsEnabled ?? false;
onlyForMembers = onlyForMembers ?? false;

This should work. You probably don't need to set the value attribute to "true" for the checkboxes in the view, but it should work either way.

nberardi May 30th, 2010 06:23 PM

fourpastmidnight,

Make sure you know what you are doing changing HttpPostOnly to HttpPost. They are not a shot by shot remake, and definitely have different functionality.

HTTP Post Only will only accept POST against the action. HTTP Post says that it prefers a POST when there is other actions with the same action name. But it will also accept GET and anything else.

Make sure you don't interpret, HttpPost as a security measure, because HttpPostOnly was a security measure against other types of requests. You will need to protect your action method in a different manor against GET and other requests that you don't want to allow.

Nick

fourpastmidnight May 30th, 2010 09:19 PM

MVC 2.0 Change Document says otherwise?
 
Nick:

You know, I followed the discussion earlier in this post regarding this, and I agreed with you then. Now, I'm not really sure what the full story is.

Now, in version MVC 1.0, this "may" have been true (most certainly it was true for MVC 1.0 PR, which the book uses, I don't doubt that). However, in MVC 2.0, this is a new change. (maybe?) From what I've read, HttpPostAttribute only accepts HTTP POST verbs according to the changes FAQ on asp.net: http://www.asp.net/learn/whitepapers...t-mvc#_TOC3_12.

I'm open to further discussion on this, as it's important that if we're doing something for security, that it is in fact secure. There's nothing worse than a false sense of security.

Of course, I guess I should crack open .Net Reflector and look at the HttpPostAttribute and compare it to ManagedFusion.Web.Mvc.HttpPostOnlyAttribute.

fourpastmidnight May 30th, 2010 09:32 PM

Chapter 6: ViewArticle.aspx, Comment and foreach problem
 
I'd appreciate if anyone could help me out.

I'm at the part in the book where I'm creating the ViewArticle view (ViewArticle.aspx). I'm having a problem with the following code:

Code:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site/Site.Master" Inherits="System.Web.Mvc.ViewPage<TheBeerHouse.Models.Article>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <!-- There's some intermediate HTML/ASP.NET MVC nuggets here which are not pertinent. -->
    <div id="article-comments">
        <h3>Comments</h3>
        <% foreach (TheBeerHouse.Models.Comment comment in ViewData.Model.Comments)
          {
              Html.RenderPartial("CommentItem", comment);
          }
        %>
    </div>
    <!-- More stuff here. -->
</asp:Content>

The problem is with the foreach loop. First, the source code from codeplex doesn't even use the fully qualified name for Comment in the foreach loop, but if I don't use the fully qualified name, Comment is not recognized as a type. This occurs in several files in my project. I haven't had a problem with this until now. The foreach loop complains that it "cannot operate on variables of type System.Data.Linq.EntitySet<TheBeerHouse.Models.Com ment> because ....[it] does not contain a public definition for GetEnumerator". Um, first, I thought that what had to implement GetEnumerator was TheBeerHouse.Models.Article.Comments.... I'm confused.

nberardi May 31st, 2010 12:05 AM

Quote:

Originally Posted by fourpastmidnight (Post 258567)
Now, in version MVC 1.0, this "may" have been true (most certainly it was true for MVC 1.0 PR, which the book uses, I don't doubt that). However, in MVC 2.0, this is a new change. (maybe?) From what I've read, HttpPostAttribute only accepts HTTP POST verbs according to the changes FAQ on asp.net: http://www.asp.net/learn/whitepapers...t-mvc#_TOC3_12..

You should take a look at it again. It does only allow HTTP POST requests now, if defined. So in that case it is working fine.

The problem, that you are failing to realize, is that it doesn't follow HTTP standards, specifically around the HTTP 405 method not allowed.

http://www.checkupdown.com/status/E405.html

Which is critical for some REST applications to function. Currently it returns an HTTP 404 Not Found response, which is totally wrong if they are trying to shoot for standards.

http://www.checkupdown.com/status/E404.html

If you read 404 specs, it only applies to the URL, not the method used against the URL. The 405 response defines how the method should be responded to if not currently defined or allowed.

This has been my biggest problem with the MVC team, they sit back and talk about how standards compliment MVC is, and everybody is willing to just gobble it up, because they really want to believe it is true, and with out doing any research to make sure it is true. Don't worry you are not the only one who has made this mistake. And in the process of everybody talking about how standards compliment MVC is they are actually bastardizing the standard because none of them over their want to read or take the time to actually understand something as basic as HTTP status codes.

So if you want standards compliment code you still have to use my HttpPostOnly attribute, if you don't care, then use HttpPost.

As I stated before, we never mentioned ManagedFusion.dll in the whole book, because we totally expected it to be obsolete by publishing, but the MVC team has continued to let us down for a couple small features that makes your application that much more in line with standards. And me and Al weren't going to compromise on putting out crap, just because the MVC team doesn't understand the difference between a 404 and a 405.

fourpastmidnight May 31st, 2010 09:13 AM

HTTP Post Only Actions
 
Nick:

Wow! Thanks for the reply. Apparently, there is much more to this than just making sure you accept only HTTP POST verbs. I'm relatively "new" to Web programming. I understand the basics (which is probably what 80% of all "web programmers" really understand, if you think about it). I had no idea that the 404 would be returned through MS's attribute.

I am aware of most of the HTTP error codes, but 405 is not one of them. That's interesting. Thanks again, I am truly here to learn. Guess I should get my hands on the HTTP spec and check MS's code for what they're doing. It's unfortunate that I can't trust them to create "compliant" code when that's what they're touting.

Guess I'll be hooking back up ManagedFusion.Web.Mvc.dll. [;)]

ulikkg June 14th, 2010 01:53 PM

error in HTML.IF
 
I want to use membership part in another project. When I followed this chapter in TheBeerHouse project I did not face but in this new project I am facing. Could somebody please help?

I created Register method inside UserController. Then without doing other Logi, ManageUsers etc methods I continued with Register View. When I run application I am getting following error:

Compiler Error Message: CS1061:
'System.Web.Mvc.HtmlHelper<object>' does not contain a definition for 'If' and no extension method 'If' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<object>' could be found (are you missing a using directive or an assembly reference?)


for


Line 13: <form method = "post" action = "<%= Url.Action("Register", "User") %>"
Line 14: <input type="hidden" name="returnUrl" value="<%= Html.If(!String.IsNullOrEmpty(Request.QueryString["returnUrl"]), () => Request.QueryString["returnUrl"])
Line 15: .ElseIf(Request.UrlReferrer != null, () => Request.UrlReferrer.ToString())
Line 16: .Else(() => String.Empty) %>"/>


Please help.

fourpastmidnight June 16th, 2010 09:05 PM

HtmlHelper.If
 
Ulikkg:

The HtmlHelper.If method is not (to my knowledge) a standard .NET Base Class Library class. IIRC, this method is in one of the ManagedFusion.Web.*.dll files that came with the source code for the book (and which IIRC is freely downloadable and usable in your own projects). If it's not in ManagedFusion, it's in one of the other 3rd Party libraries.

I hope from what I've posted you can find this method.

Even if you can't, you don't need it. That method is just a nice way of doing If/Else constructs without all the "Classic ASP"-looking spaghetti code; but don't get me wrong, in the context of ASP.NET MVC, there's nothing wrong with the following code:

Code:

<% if (/* my boolean expression condition */ ) { %>
  <!-- Put some other ASP.NET code and/or HTML here. -->
<% } else { %>
  <!-- Do something else. -->
<% } %>

HTH.

M3rcuryKT February 19th, 2011 10:40 PM

Quote:

Next, right-click the project and, IIRC, there is' an Edit Project item. In any event, it will open up the XML file that resembles your project. Locate the following XML:
What is IIRC?

Still not clear about how to fix this, I've got the same Iso3166CountryCodes problem over "Properties" (does not exist in current context)


string[] iso3166 = Properties.Resources.Iso3166CountryCodes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

Am I missing something? Any feedback is appreciated, thank you.


All times are GMT -4. The time now is 05:08 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.