Changeset 134

User picture

Author: Andrew Davey

(2009/09/01 13:40) Over 2 years ago


  

Affected files

Updated trunk/Source/SampleApplication/Controllers/HomeController.cs Download diff

133134
1
using System.Web.Mvc;
1
using System.Web.Mvc;
2
using Snooze;
2
using Snooze;
3
using Snooze.Authentication;
4
3
5
namespace SampleApplication.Controllers
4
namespace SampleApplication.Controllers
6
{
5
{
...
...
12
        {
11
        {
13
            return OK(new HomeViewModel
12
            return OK(new HomeViewModel
14
            {
13
            {
15
                Login = new LoginUrl(),
16
                BooksLink = new BooksUrl().ToString()
14
                BooksLink = new BooksUrl().ToString()
17
            }).WithXrdsHeader(); // for openid 2.0 support
15
            });
18
        }
16
        }
19
    }
17
    }
20
18

Updated trunk/Source/SampleApplication/Global.asax.cs Download diff

133134
1
using System.Web.Mvc;
1
using System.Web.Mvc;
2
using System.Web.Routing;
2
using System.Web.Routing;
3
using SampleApplication.Controllers;
3
using SampleApplication.Controllers;
4
using Snooze.Authentication;
5
using Snooze.Routing;
4
using Snooze.Routing;
6
5
7
namespace SampleApplication
6
namespace SampleApplication
...
...
13
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
12
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
14
            
13
            
15
            // Snooze provides some handy features...
14
            // Snooze provides some handy features...
16
            routes.AddOpenIdSupport();
17
            routes.AddVersionedStaticFilesSupport();
15
            routes.AddVersionedStaticFilesSupport();
18
            routes.AddIE6Support();
16
            routes.AddIE6Support();
19
17

Updated trunk/Source/Snooze/Input.cs Download diff

133134
86
            _errorMessages.Add(message);
86
            _errorMessages.Add(message);
87
        }
87
        }
88
88
89
        public override string ToString()
90
        {
91
            if (HasValue)
92
            {
93
                return Value.ToString();
94
            }
95
            else
96
            {
97
                return default(T).ToString();
98
            }
99
        }
100
89
        public static implicit operator T(Input<T> input)
101
        public static implicit operator T(Input<T> input)
90
        {
102
        {
91
            if (input.IsValid)
103
            if (input.IsValid)

Updated trunk/Source/Snooze/InputModelBinder.cs Download diff

133134
1
using System.ComponentModel;
1
using System.ComponentModel;
2
using System.Web.Mvc;
2
using System.Web.Mvc;
3
using System;
3
4
4
namespace Snooze
5
namespace Snooze
5
{
6
{
6
    public class InputModelBinder : DefaultModelBinder
7
    public class InputModelBinder : DefaultModelBinder
7
    {
8
    {
9
        public InputModelBinder(IModelBinder defaultBinder)
10
        {
11
            _innerBinder = defaultBinder;
12
        }
13
14
        IModelBinder _innerBinder;
15
8
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
16
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
9
        {            
17
        {            
10
            if (typeof(IInput).IsAssignableFrom(propertyDescriptor.PropertyType))
18
            if (typeof(IInput).IsAssignableFrom(propertyDescriptor.PropertyType))
11
            {
19
            {
12
                var binder = this.Binders.GetBinder(propertyDescriptor.PropertyType);
20
                BindInputProperty(controllerContext, bindingContext, propertyDescriptor);
13
                var context = new ModelBindingContext
14
                {
15
                    ModelState = bindingContext.ModelState,
16
                    ModelType = propertyDescriptor.PropertyType,
17
                    ValueProvider = bindingContext.ValueProvider
18
                };
19
                var input = binder.BindModel(controllerContext, context);
20
                SetProperty(controllerContext, bindingContext, propertyDescriptor, input);
21
            }
21
            }
22
            else
22
            else
23
            {
23
            {
24
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
24
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
25
            }
25
            }
26
        }
26
        }
27
28
        void BindInputProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
29
        {
30
            var binder = this.Binders.GetBinder(propertyDescriptor.PropertyType);
31
            if (binder is InputModelBinder) binder = _innerBinder;
32
33
            var context = new ModelBindingContext
34
            {
35
                ModelName = propertyDescriptor.Name,
36
                ModelState = bindingContext.ModelState,
37
                ModelType = typeof(string),
38
                ValueProvider = bindingContext.ValueProvider
39
            };
40
            var value = (string)binder.BindModel(controllerContext, context);
41
            var input = (IInput)Activator.CreateInstance(propertyDescriptor.PropertyType);
42
            if (value != null)
43
            {
44
                input.RawValue = value;
45
            }
46
            SetProperty(controllerContext, bindingContext, propertyDescriptor, input);
47
        }
27
    }
48
    }
28
}
49
}

Updated trunk/Source/Snooze/IResourceFormatter.cs Download diff

133134
4
{
4
{
5
    public interface IResourceFormatter
5
    public interface IResourceFormatter
6
    {
6
    {
7
        bool CanFormat(object resource, string mimeType);
7
        bool CanFormat(ControllerContext context, object resource, string mimeType);
8
        void Output(ControllerContext context, object resource, string contentType);
8
        void Output(ControllerContext context, object resource, string contentType);
9
    }
9
    }
10
}
10
}

Updated trunk/Source/Snooze/JsonFormatter.cs Download diff

133134
7
{
7
{
8
    public class JsonFormatter : IResourceFormatter
8
    public class JsonFormatter : IResourceFormatter
9
    {
9
    {
10
        public bool CanFormat(object resource, string mimeType)
10
        public bool CanFormat(ControllerContext context, object resource, string mimeType)
11
        {
11
        {
12
            return resource != null && mimeType == "application/json";
12
            return resource != null && mimeType == "application/json";
13
        }
13
        }

Updated trunk/Source/Snooze/ResourceController.cs Download diff

133134
44
            return new ResourceResult(304, null);
44
            return new ResourceResult(304, null);
45
        }
45
        }
46
46
47
        public virtual ActionResult Redirect(Url url)
48
        {
49
            return Redirect(url.ToString());
50
        }
51
52
        public virtual ActionResult TemporaryRedirect(Url url)
53
        {
54
            return new ResourceResult(307, url.ToString()).WithHeader("Location", url.ToString());
55
        }
56
57
        public virtual ActionResult TemporaryRedirect(string url)
58
        {
59
            return new ResourceResult(307, url).WithHeader("Location", url);
60
        }
61
47
        public virtual ResourceResult BadRequest()
62
        public virtual ResourceResult BadRequest()
48
        {
63
        {
49
            return new ResourceResult(403, null);
64
            return new ResourceResult(400, null);
50
        }
65
        }
51
66
52
        public virtual ResourceResult BadRequest(object errorResource)
67
        public virtual ResourceResult BadRequest(object errorResource)
53
        {
68
        {
54
            return new ResourceResult(403, errorResource);
69
            return new ResourceResult(400, errorResource);
55
        }
70
        }
56
71
57
        public virtual ResourceResult NotFound()
72
        public virtual ResourceResult NotFound()

Updated trunk/Source/Snooze/ResourceFormatters.cs Download diff

133134
16
            // So we add an explicitly typed ViewFormatter first.
16
            // So we add an explicitly typed ViewFormatter first.
17
            _formatters.Add(new ViewFormatter("application/xhtml+xml"));
17
            _formatters.Add(new ViewFormatter("application/xhtml+xml"));
18
            _formatters.Add(new ViewFormatter("*/*")); // similar reason for this.
18
            _formatters.Add(new ViewFormatter("*/*")); // similar reason for this.
19
            _formatters.Add(new StringFormatter());
20
            _formatters.Add(new JsonFormatter());
19
            _formatters.Add(new JsonFormatter());
21
            _formatters.Add(new XmlFormatter());
20
            _formatters.Add(new XmlFormatter());
22
            _formatters.Add(new ViewFormatter());
21
            _formatters.Add(new ViewFormatter());
22
            _formatters.Add(new StringFormatter());
23
        }
23
        }
24
24
25
        public static IList<IResourceFormatter> Formatters
25
        public static IList<IResourceFormatter> Formatters

Updated trunk/Source/Snooze/ResourceResult.cs Download diff

133134
82
            }
82
            }
83
83
84
            var acceptTypes = ParseAcceptTypes(context.HttpContext.Request.AcceptTypes);
84
            var acceptTypes = ParseAcceptTypes(context.HttpContext.Request.AcceptTypes);
85
            var formatter = FindFormatter(acceptTypes);
85
            var formatter = FindFormatter(context, acceptTypes);
86
            if (formatter == null)
86
            if (formatter == null)
87
            {
87
            {
88
                context.HttpContext.Response.StatusCode = 406; // not acceptable
88
                if (Resource is string)
89
                {
90
                    context.HttpContext.Response.Output.Write(Resource);
91
                }
92
                else
93
                {
94
                    context.HttpContext.Response.StatusCode = 406; // not acceptable
95
                }
89
                return;
96
                return;
90
            }
97
            }
91
            formatter.Output(context, Resource, ContentType);
98
            formatter.Output(context, Resource, ContentType);
...
...
125
            }
132
            }
126
        }
133
        }
127
134
128
        IResourceFormatter FindFormatter(IEnumerable<string> acceptTypes)
135
        IResourceFormatter FindFormatter(ControllerContext context, IEnumerable<string> acceptTypes)
129
        {
136
        {
130
            if (ContentType != null) // Controller action forced the content type.
137
            if (ContentType != null) // Controller action forced the content type.
131
            {
138
            {
132
                EnsureContentTypeIsMimeType();
139
                EnsureContentTypeIsMimeType();
133
                return ResourceFormatters.Formatters.FirstOrDefault(f => f.CanFormat(Resource, ContentType));
140
                return ResourceFormatters.Formatters.FirstOrDefault(f => f.CanFormat(context, Resource, ContentType));
134
            }
141
            }
135
142
136
            foreach (var formatter in ResourceFormatters.Formatters)
143
            foreach (var formatter in ResourceFormatters.Formatters)
137
            {
144
            {
138
                foreach (var acceptType in acceptTypes)
145
                foreach (var acceptType in acceptTypes)
139
                {
146
                {
140
                    if (formatter.CanFormat(Resource, acceptType)) return formatter;
147
                    if (formatter.CanFormat(context, Resource, acceptType)) return formatter;
141
                }
148
                }
142
            }
149
            }
143
            return null;
150
            return null;

Updated trunk/Source/Snooze/Snooze.csproj Download diff

133134
65
    <Compile Include="FutureAction.cs" />
65
    <Compile Include="FutureAction.cs" />
66
    <Compile Include="IInput.cs" />
66
    <Compile Include="IInput.cs" />
67
    <Compile Include="Input.cs" />
67
    <Compile Include="Input.cs" />
68
    <Compile Include="InputModelAttribute.cs" />
69
    <Compile Include="InputModelBinder.cs" />
68
    <Compile Include="InputModelBinder.cs" />
70
    <Compile Include="IResourceFormatter.cs" />
69
    <Compile Include="IResourceFormatter.cs" />
71
    <Compile Include="JsonFormatter.cs" />
70
    <Compile Include="JsonFormatter.cs" />

Updated trunk/Source/Snooze/StringFormatter.cs Download diff

133134
4
{
4
{
5
    public class StringFormatter : IResourceFormatter
5
    public class StringFormatter : IResourceFormatter
6
    {
6
    {
7
        public bool CanFormat(object resource, string mimeType)
7
        public bool CanFormat(ControllerContext context, object resource, string mimeType)
8
        {
8
        {
9
            if (resource != null && resource.GetType() == typeof(string)) return true;
9
            if (resource != null && resource.GetType() == typeof(string)) return true;
10
            if (mimeType == "text/plain") return true;
10
            if (mimeType == "text/plain") return true;

Updated trunk/Source/Snooze/ViewFormatter.cs Download diff

133134
15
15
16
        string _targetMimeType;
16
        string _targetMimeType;
17
17
18
        public bool CanFormat(object resource, string mimeType)
18
        public bool CanFormat(ControllerContext context, object resource, string mimeType)
19
        {
19
        {
20
            return (_targetMimeType == mimeType) || (_targetMimeType == null);
20
            return ((_targetMimeType == mimeType) || (_targetMimeType == null)) 
21
                && FindView(context, resource).View != null;
21
        }
22
        }
22
23
23
        public void Output(ControllerContext context, object resource, string contentType)
24
        public void Output(ControllerContext context, object resource, string contentType)
24
        {
25
        {
26
            if (contentType != null)
27
            {
28
                context.HttpContext.Response.ContentType = contentType;
29
            }
30
31
            var result = FindView(context, resource);
32
            if (result.View != null)
33
            {
34
                result.View.Render(
35
                    new ViewContext(
36
                        context,
37
                        result.View,
38
                        new ViewDataDictionary(resource),
39
                        new TempDataDictionary()
40
                    ),
41
                    context.HttpContext.Response.Output
42
                );
43
            }
44
        }
45
46
        private ViewEngineResult FindView(ControllerContext context, object resource)
47
        {
25
            var viewName = GetViewName(resource);
48
            var viewName = GetViewName(resource);
26
            var result = ViewEngines.Engines.FindView(context, viewName, null);
49
            var result = ViewEngines.Engines.FindView(context, viewName, null);
27
            context.HttpContext.Response.ContentType = contentType;
50
            return result;
28
            result.View.Render(
29
                new ViewContext(
30
                    context, 
31
                    result.View, 
32
                    new ViewDataDictionary(resource), 
33
                    new TempDataDictionary()
34
                ), 
35
                context.HttpContext.Response.Output
36
            );
37
        }
51
        }
38
52
39
        string GetViewName(object resource)
53
        string GetViewName(object resource)

Updated trunk/Source/Snooze/XmlFormatter.cs Download diff

133134
7
{
7
{
8
    public class XmlFormatter : IResourceFormatter
8
    public class XmlFormatter : IResourceFormatter
9
    {
9
    {
10
        public bool CanFormat(object resource, string mimeType)
10
        public bool CanFormat(ControllerContext context, object resource, string mimeType)
11
        {
11
        {
12
            return resource != null && mimeType.Contains("xml");
12
            return resource != null && mimeType.Contains("xml");
13
        }
13
        }