Home

History Key

  • New content
  • Removed content

Recent Versions

Choose two versions to compare, or click the link to view it.

  1. 27. about 3 years by andrewdavey
  2. 26. about 3 years by andrewdavey
  3. 25. about 3 years by andrewdavey
  4. 24. about 3 years by andrewdavey
  5. 23. about 3 years by andrewdavey
  6. 22. about 3 years by andrewdavey
  7. 21. about 3 years by andrewdavey
  8. 20. about 3 years by andrewdavey
  9. 19. about 3 years by andrewdavey
  10. 18. about 3 years by andrewdavey
  11. 17. about 3 years by andrewdavey
  12. 16. about 3 years by andrewdavey
  13. 15. about 3 years by andrewdavey
  14. 14. about 3 years by andrewdavey
  15. 13. about 3 years by andrewdavey
  16. 12. about 3 years by andrewdavey
  17. 11. about 3 years by andrewdavey
  18. 10. about 3 years by andrewdavey
  19. 9. about 3 years by andrewdavey
  20. 8. about 3 years by andrewdavey
  21. 7. about 3 years by andrewdavey
  22. 6. about 3 years by andrewdavey
  23. 5. about 3 years by andrewdavey
  24. 4. about 3 years by andrewdavey
  25. 3. about 3 years by andrewdavey
  26. 2. about 3 years by andrewdavey
  27. 1. over 4 years by andrewdavey
 

A RESTful Framework for ASP.NET MVC

ASP.NET MVC is far too reliant on magic-strings. Snooze is a thin layer on top of the ASP.NET MVC framework that brings some sanity back!
Snooze makes URLs a first-class, strongly-typed concept. Controllers contain methods that handle HTTP requests for URLs.

Snooze is still under development, but the following samples are from working code.

Enjoy!

In Global.asax, routes are mapped using strongly-typed expressions

routes.Map<BooksUrl>(b => "books");
routes.Map<BookUrl>(b => "book/" + b.BookId); // route will be "book/{BookId}"
routes.Map<BookCommentsUrl>(c => "comments"); // route will be "book/{BookId}/comments"

Those expressions reference these Url classes.

Notice that BookCommentsUrl is a SubUrl of BookUrl. This means is has access to BookUrl via its Parent property.

public class BooksUrl : Url { }

public class BookUrl : Url
{
    public string BookId { get; set; }
}

public class BookCommentsUrl : SubUrl<BookUrl> { }

A single controller class can handle requests for any of these Urls. Dispatch is done using the request HTTP method.

public class BookController : ResourceController
{
    public ActionResult Get(BooksUrl url)
    {
        var books = LoadBooksXml();
        // Notice how we can construct URLs directly...
        var links = from book in books.Element("catalog").Elements("book")
                    select new BookUrl { BookId = book.Attribute("id").Value };
 
        return View(OK(new BooksViewModel
        {
            BookLinks = links.ToArray()
        });
    }
 
    public ActionResult Get(BookUrl url)
    {
        var books = LoadBooksXml();
        var book = books.Element("catalog").Elements("book").FirstOrDefault(e => e.Attribute("id").Value == url.BookId);
        return View(OK(new BookViewModel
        {
            Author = book.Element("author").Value,
            Title = book.Element("title").Value
        });
    }
 
    public ActionResult Post(BookCommentsUrl url, string comment)
    {
        // Can access book id using: url.Parent.BookId
        throw new NotImplementedException();
    }
 
    private XDocument LoadBooksXml()
    {
        return XDocument.Load(Server.MapPath("~/App_Data/Books.xml"));
    }
}
public class BooksViewModel
{
    public BookUrl[] BookLinks { get; set; }
}
 
public class BookViewModel
{
    public string Author { get; set; }
    public string Title { get; set; }
}