How to use AllowDefault for default Controller ?

Jun 1, 2011 at 3:32 PM
Edited Jun 1, 2011 at 3:37 PM

Hello,

This is a question for mef.contrib.mvc3, but i didn't find the specific codeplex place so i post here.

I want to have an explicite default controller if there are no part found:

 

public interface IHelloService
    {
        string SayHello();
    }

    public class HomeController : Controller
    {
        IHelloService _service { get; set; }

        [ImportingConstructor]
        public HomeController([Import(AllowDefault = true)] IHelloService service)
        {
            if (service == null)
                service = new ServiceTest();
            _service = service;
        }       

        public ActionResult Index()
        {
            ViewBag.Message = _service.SayHello();

            return View();
        }
    }

    public class ServiceTest : IHelloService
    {

        public string SayHello()
        {
            return "test";
        }
    }

 

 

but if there are no Export i've got this message :

The IControllerFactory 'System.Web.Mvc.DefaultControllerFactory' did not return a controller for the name 'Home'.

How can i have a default conroller working if no DLL were found in the bin ?

Thanx for the answer.

(French inside so sorry for my poor english :) )

Jun 3, 2011 at 7:26 AM
 

Good one :-) We are currently not taking the "AllowDefault" parameter into account. I'm checking with pwlodek to see how we best handle this situation.

Jun 3, 2011 at 9:04 AM
Edited Jun 3, 2011 at 9:22 AM

An other one if you want :)

I try to find a work around with implementing the default Controller action as a Part, but without any success ^^ :

 

IHelloService[] _service { get; set; }

        [ImportingConstructor]
        public HomeController([ImportMany] IHelloService[] service)
        {
            _service = service;
        }       

        public ActionResult Index(Nullable<int> index)
        {
            if (!index.HasValue)
                index = default(int);

            ViewBag.Message = _service[index.Value].SayHello();

            return View();
        }
    }

    [Export(typeof(IHelloService))]
    public class ServiceTest : IHelloService
    {

        public string SayHello()
        {
            return "test";
        }
    }

 

The "_service" is created (not null) but none of my Parts are inside (Count=0) so "ViewBag.Message = _service[index.Value].SayHello();" throw an OutOfRangeException (with the value 0)...

 

Please tell me if you know an other option...

 

Thank's

 

ps: My goal is to have some Part (3 ou 4) by Controller, and then decide with a web service the good to execute, but some other could have no External Part, and then, i want to provide a default one

ps2: Where can i find the mef.contrib.mvc3 sources?