IronPython on ASP.NET MVC
Asked Answered
K

3

24

Has anyone tried ASP.NET MVC using IronPython? Having done a lot of Python development recently, it would be nice to continue with the language as I go into a potential ASP.NET MVC project.

I'm especially interested in exploiting the dynamic aspects of Python with .NET features such as LINQ and want to know if this will be possible. The other route that may be viable for certain dynamic programming would be C# 4.0 with its dynamic keyword.

Thoughts, experiences?

Knighthood answered 14/1, 2009 at 3:45 Comment(0)
Q
14

Yes, there is an MVC example from the DLR team.

You might also be interested in Spark.

Qualified answered 14/1, 2009 at 14:16 Comment(3)
The linked samples seem to be about WebForms rather than ASP.Net MVCFirewood
@Abhijit, unfortunately CodePlex URLs seem to have a limited lifetime. In the 2+ years since I posted this answer, they broke the link.Qualified
Is anyone using this in production?Megmega
W
8

Using IronPython in ASP.NET MVC: http://www.codevoyeur.com/Articles/Tags/ironpython.aspx

this page contains following articles:

  • A Simple IronPython ControllerFactory for ASP.NET MVC
  • A Simple IronPython ActionFilter for ASP.NET MVC
  • A Simple IronPython Route Mapper for ASP.NET MVC
  • An Unobtrusive IronPython ViewEngine for ASP.NET MVC
Wang answered 19/11, 2010 at 8:59 Comment(0)
C
5

I'm currently working on this. It already supports a lot of things: https://github.com/simplic-systems/ironpython-aspnet-mvc

more information on this:

Import the aspnet module

import aspnet

You can write your own controller

class HomeController(aspnet.Controller):

    def index(self):
        return self.view("~/Views/Home/Index.cshtml")

You can automatically register all controller

aspnet.Routing.register_all()

You can use different http-methods

@aspnet.Filter.httpPost
    def postSample(self):
        return self.view("~/Views/Home/Index.cshtml")

And there is much more. Here is a very short example

# ------------------------------------------------
# This is the root of any IronPython based
# AspNet MVC application.
# ------------------------------------------------

import aspnet

# Define "root" class of the MVC-System
class App(aspnet.Application):

    # Start IronPython asp.net mvc application. 
    # Routes and other stuff can be registered here
    def start(self):

        # Register all routes
        aspnet.Routing.register_all()

        # Set layout
        aspnet.Views.set_layout('~/Views/Shared/_Layout.cshtml')

        # Load style bundle
        bundle = aspnet.StyleBundle('~/Content/css')
        bundle.include("~/Content/css/all.css")

        aspnet.Bundles.add(bundle)

class HomeController(aspnet.Controller):

    def index(self):
        return self.view("~/Views/Home/Index.cshtml")

    def page(self):
        # Works also with default paths
        return self.view()

    def paramSample(self, id, id2 = 'default-value for id2'):
        # Works also with default paths
        model = SampleModel()
        model.id = id
        model.id2 = id2
        return self.view("~/Views/Home/ParamSample.cshtml", model)

    @aspnet.Filter.httpPost
    def postSample(self):
        return self.view("~/Views/Home/Index.cshtml")

class SampleModel:
    id = 0
    id2 = ''

class ProductController(aspnet.Controller):

    def index(self):
        return self.view("~/Views/Product/Index.cshtml")
Craquelure answered 23/2, 2016 at 16:41 Comment(2)
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Oeuvre
@BhargavRao answer is improved, does it now fits the requirements?Craquelure

© 2022 - 2024 — McMap. All rights reserved.