Skip to content

Commit ffea2ca

Browse files
author
Anton Wieslander
committed
mvc
1 parent 7332e74 commit ffea2ca

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

MVC/mvc_the_problem.linq

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var uri = new Uri("http://localhost/home/index");
6+
7+
uri.AbsolutePath.Split("/").Skip(1).Dump();
8+
if(uri.AbsolutePath.StartsWith("/home/index")){
9+
// execute some loginc
10+
}
11+
}

MVC/mvc_the_solution.linq

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<Query Kind="Program">
2+
<Namespace>System.Web</Namespace>
3+
</Query>
4+
5+
void Main()
6+
{
7+
var uri = new Uri("http://localhost/home/index?msg=Hello&num=5");
8+
9+
var container = new MvcContainer();
10+
var result = container.Resolve(uri);
11+
result.Dump();
12+
}
13+
14+
public class MvcContainer {
15+
16+
List<Type> controllerTypes = new List<Type>();
17+
18+
public MvcContainer()
19+
{
20+
var controllerType = typeof(Controller);
21+
22+
controllerTypes = controllerType.Assembly.GetTypes()
23+
.Where(type => !type.IsAbstract
24+
&& controllerType.IsAssignableFrom(type))
25+
.ToList();
26+
}
27+
28+
public object Resolve(Uri uri){
29+
var controller = getController(uri);
30+
var action = getAction(controller, uri);
31+
var parameters = getParameters(action, uri);
32+
return action.Invoke(controller, parameters);
33+
}
34+
35+
private object[] getParameters(MethodInfo methodInfo, Uri uri)
36+
{
37+
var parameterInfos = methodInfo.GetParameters().ToList();
38+
if(parameterInfos.Count == 0){
39+
return null;
40+
}
41+
var results = new object[parameterInfos.Count];
42+
43+
var query = HttpUtility.ParseQueryString(uri.Query);
44+
45+
for(int i = 0; i < parameterInfos.Count; i++){
46+
var info = parameterInfos[i];
47+
var type = parameterInfos[i].ParameterType;
48+
var value = query[info.Name];
49+
if(type == typeof(String)){
50+
results[i] = value;
51+
}
52+
else if (type == typeof(Int32))
53+
{
54+
results[i] = Int32.Parse(value);
55+
}
56+
}
57+
58+
return results;
59+
}
60+
61+
private MethodInfo getAction(Controller controller, Uri uri)
62+
{
63+
var action = uri.AbsolutePath.Split('/').Last();
64+
65+
return controller.GetType().GetMethods()
66+
.FirstOrDefault(x => x.Name.Equals(action,
67+
StringComparison.InvariantCultureIgnoreCase));
68+
}
69+
70+
public Controller getController(Uri uri){
71+
var controllerType = controllerTypes
72+
.FirstOrDefault(x => uri.AbsolutePath
73+
.StartsWith($"/{x.Name.Replace("Controller", "")}",
74+
StringComparison.InvariantCultureIgnoreCase));
75+
76+
77+
return (Controller) Activator.CreateInstance(controllerType, null);
78+
}
79+
80+
}
81+
82+
public abstract class Controller{}
83+
84+
public class HomeController : Controller {
85+
86+
public string Index(int num, string msg)
87+
{
88+
return $"Hello World {msg} - {num}";
89+
}
90+
91+
public string Test()
92+
{
93+
return "Hello Test";
94+
}
95+
}
96+
97+
public class TestController : Controller
98+
{
99+
100+
public string Index()
101+
{
102+
return "Test World";
103+
}
104+
105+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Full Playlist can be found [here](https://www.youtube.com/playlist?list=PLOeFnOV
1010
- [How to Use Async AWait Task](https://youtu.be/3GhKdDCvtKE)
1111
- [Semaphore](https://youtu.be/GKjM4AX8NME)
1212
- [Channels](https://youtu.be/E0Ld7ZgE4oY)
13+
- [MVC](https://youtu.be/u4O-b1BJg98)

0 commit comments

Comments
 (0)