tags:

views:

294

answers:

8
+3  Q: 

Website in pure C#

I want to learn how to make a website with C#. I know PHP, but I like the C# language better. I have tried learning how to make a website in ASP.NET which so far has really frustrated me because I really don't like ASP at all (even less than I like PHP). Something about it drives me completely insane to the point where I am having a hard time forcing myself to learn it. Is there a way to just immediately transfer control away from ASP to C# and then do everything from C#, including outputting HTML and other stuff when necessary like you would in PHP? In other words I like the PHP way of website programming but prefer the C# language. Any help will be appreciated.

+9  A: 

Take a look at asp.net mvc: http://www.asp.net/mvc/.

It's much closer to the metal than asp.net webforms and should feel more comfortable if you're coming from a PHP background.

There's lots of good tutorials here: http://www.asp.net/Learn/mvc/

jonnii
I agree that ASP.NET MVC is a great framework, however I don't see why the OP would feel more comfortable with it...
Thomas Levesque
Thanks for this! I have been trying out the ASP.NET MVC framework and I like it a lot. Going through those tutorials was a way better experience than standard ASP.NET tutorials. What I discovered was that it's not that I don't like ASP code, I just don't like to start and end there. When you conceptually start in C# and then decide when to transfer control to ASP, and even then only for the View, ASP is actually nice like that. So far the MVC framework has been easy to learn, use and customize.
INTPnerd
I'm glad I could help!
jonnii
+6  A: 

Yes, you can use HttpHanders to handle all your requests. An HttpHandler is a class that can receive a request directly, and handle it all with code. It is often used to catch a request for an image, and serve it for a database, but you could certainly use it to do what you want.

Check out the MSDN introduction to using HttpHandlers. Please post a comment if you have any more questions about it.

ASP.NET MVC is another option, but that still uses ASPX markup by default. You could, potentially, find another view engine that you like more. I don't know anything about that, though. (edited - thanks Joel!)

Edit: Keep in mind that you are technically still within an ASP.NET project when using HttpHandlers, but that's just to get the Request/Response/Server Context/etc Framework running around you. You can still work 100% with C# code.

JoshJordan
Seriously... are we recommending to a newcomer who hasn't got his head round asp.net that generic handlers are the way to go? Although this technically answers the question, I feel compelled to warn that implementing websites in this fashion would be seriously detrimental to one's health, given the wealth of other options that could bring the OP to a higher level of productivity.
spender
That is what I'mg suggesting, yes. You ask a question, you get the answer. If he wants to use code exclusively, this is how it can be done. One of the reasons many people can answer questions is so that we can get many perspectives, and this one certainly belongs here.
JoshJordan
@spender: As stated below, generic handlers and httphandlers are two very different things. A php (or even java) person is actually going to feel a lot more at home using a generic handler than going the full .net route.
Chris Lively
You don't have to use ASPX markup with ASP.NET MVC. You can plug in a different View engine - there are a half-dozen or so to choose from.
Joel Mueller
Sorry. My bad with HttpHandler/Generic Handlers. Some serious misreading going on after a long week.
spender
@Chris: I wouldn't say they're "very different things"... a generic handler is just an implementation of IHttpHandler, with a .ashx file to avoid the trouble of registering the handler, so they're basically the same, unless I'm missing something...
Thomas Levesque
Thanks for this answer. I guess this is technically the correct answer to my question and I do appreciate this information. I will stick with the MVC framework for now, but if I ever get too disgruntled with that I may try using HTTP Handlers and create my own framework. I do create my own frameworks/tools sometimes which can be done relatively quickly and give you a huge productivity and code quality boost if you know what you are doing.
INTPnerd
+1  A: 

ASP.NET MVC might be a good option : www.asp.net/mvc

Yassir
+2  A: 

You can also use Generic Handlers (.ashx). These supply you with a httpcontext and it's completely up to you what you do with it.

Chris Lively
See my comment in JoshJordan's answer
spender
Generic handlers via the .ashx are not the same as httphandlers. A generic handler is pretty close to what an aspx page is without all of the page lifecycle stuff.
Chris Lively
Generic handlers, is of course one of the correct answers here... However, of the list of ways one might implement a website in .net, one of the poorest would be writing it with generic handlers and inevitably reinventing framework that already exists. To me the point of moving to a new platform is because it offers more benefit/bang per buck. Shifting from PHP to generic handlers really seems to be a sideways "promotion".
spender
+1  A: 

"I really don't like ASP at all (even less than I like PHP)" followed by "I like the PHP way". It sounds like you're having trouble breaking with the old and have become somewhat conflicted.

I felt the same coming from classic asp to asp.net, and traditional asp.net sites still give me a lot of grief. When you're used to finer grained control, it can be hard to get your head round more "frameworkey" ways of doing things. As said before, give asp.net MVC a try. IMO it offers the right balance and for me is a breath of fresh air after Asp.net.

spender
+4  A: 

Just don't use any server-side control, put the code directly in the ASPX file (between <% and %>), and it should feel just like PHP (or classic ASP)... You could also do it with an HttpHandler, but in that case you will lose the ability to write inline HTML ; you would have to output all the markup using Response.Write, which isn't exactly convenient...

Note that I strongly advise against any of these approaches, that's just the answer to your question ;)

Thomas Levesque
+1 for answer and warning
spender
+1  A: 

It's not a particularly good option, but you could use C# with CGI if you want to go the simple route.

Lucas Jones
+1. I've had to do this before to meet a rather insane requirement a client had. It's not pretty, and if you can avoid it I'd recommend that, but it can be made to work.
chsh
I actually wrote an MVC framework with mod_rewrite and C# CGI.
Lucas Jones
+1  A: 

You can do most things in ASP.net using fairly pure C#. Early on I relied heavily on the ASP.net controls, dragging and dropping most of my pages with little snippets of code here and there to make controls do what I wanted. I really didn't like it, and it make me think of coding ASP.net as a completely distinct thing from C# programming(Ok it was vb at the time- Don't Judge me!).

Now almost all of my code- and html generation is handled by various custom classes. My code-behind pages simply connect the page specific inputs and event to my utillity classes. Likewise i use inline code for output- I simply invoke a function on my code behind that gathers the necessary variable and lets my classes do the heavy lifting.

I definitely find that some of the ASP.net specific controls are useful and I do use them from time to time. I did, however, find that changing the way I put my code together gave me a whole different idea of the language.

apocalypse9