1

I'm writing a console application which I'd like to apply MVC pattern. Since ASP.net MVC is already a mature framework, I wonder if I can use it for my console application instead of reinventing the wheel.

For instance, can i inherit from Microsoft.AspNet.Mvc.Controller to derive my own controller class?

I have done some research on Google but couldn't find anything specific to this question.

tereško
  • 58,060
  • 25
  • 98
  • 150
james
  • 1,107
  • 14
  • 29

2 Answers2

5

I'm writing a console application which I'd like to apply MVC pattern. Since ASP.net MVC is already a mature framework, I wonder if I can use it for my console application instead of reinventing the wheel.

No, you'd rather not. ASP.NET MVC is designed for the web and the HTTP protocol.

For instance, can i inherit from Microsoft.AspNet.Mvc.Controller to derive my own controller class?

You can inherit but it would be a complete overkill to write a hosting environment for it that simulates the HTTP world for which this is designed.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Many thanks, @Darin. Could you please elaborate a bit more on why this is an overkill? e.g. what will happen if I do derive something from AspNet.Mvc classes? – james Dec 28 '15 at 17:26
  • 3
    Nothing would happen. You will not be able to do much with it it because you will need to write an implementation for the abstract class: [`HttpContextBase`](https://msdn.microsoft.com/en-us/library/system.web.httpcontextbase(v=vs.110).aspx). Obviously in order to do that you will need to write an implementation for the gazzilion of other abstract classes that this one depends on like HttpRequestBase, HttpResponseBase, HttpSessionStateBase, ... So you will very quickly realize that this is not something you wanna be doing. – Darin Dimitrov Dec 28 '15 at 17:28
1

Writing ASP.NET MVC console application means that you are doing self-host instead of hosting it in a web server like IIS. It is possible but it's non-trivial. There are several options:

  1. Create MVC 6 application and use dnx to self-host
  2. Take advantage of IIS 7 Hostable Web Core in case your application will work only on Windows Vista+
  3. Write with NancyFx or similar framework

EDIT: Apparently I misunderstood the question: No, there is no point in using MVC framework in cases other than web applications working over http. But I'll leave this answer in case someone will open this when searching for I thought this question is about.

Community
  • 1
  • 1
Orif Khodjaev
  • 1,080
  • 1
  • 13
  • 34