1

I am using the following structure where I have used the following base class structure to implement the view.

public class Entity1Controller : ClassHelper<Entity1, Enitty1ViewModel>
{
    public Entity1Controller(IObject obj1<Entity1>, IObject1 obj2)
    {
    } 
}

public abstract class ClassHelper<TEntity>, TEntityViewModel> : ClassUtility<TEntity, TEntityViewModel>
{
    public readonly IObject obj1<TEntity>;
    public readonly IObject1 obj2
    public ClassHelper(IObject obj1<TEntity>, IObject1 obj2)
    {
         this.obj1 = obj1;
         this.obj2 = obj2;
    } 
}

public class ClassUtility<TEntity, TEntityViewModel> : Controller
{
    public readonly IObject obj1<TEntity>;
    public readonly IObject1 obj2
    public ClassHelper(IObject obj1<Entity1>, IObject1 obj2)
    {
         this.obj1 = obj1;
         this.obj2 = obj2;
    }

    public ActionResult Index()
    {
        return View();
    }


    [Route("Add")]
    [Route("Edit/{id:int:min(1)}")]
    public ActionResult AddorEditEntity(int id = 0)
    {
        if (id > 0)
        {
            //some code and get the entity
            return PartialView("_AddorEditPartial", entity);
        }

        return PartialView("_AddorEditPartial", new TEntityViewModel());
    }

    //Other Methods
}

The Webpage at www.localhost.com/Entity1 give result of Index view [as It should be], but When I tried to search for url with attribute routing eg. www.localhost.com/Entity1/Add or www.localhost.com/Entity1/Edit/1 or www.localhost.com/Entity1/GetAll and so do other methods, They do not omit any result.

If I use the convention routing in my address [rather than www.localhost.com/Entity1/Add, using the www.localhost.com/Entity1/AddorEditEntity], This application render the view and show the web result.

If I define the same methods directly from Controller i.e. using inherit the Controller directly to Entity1Controller [for ex. public class Entity1Controller : Controller, rather than base inheritance model shown above], the attribute routing works file without hitch.

I tried to google it, and come to know able DefaultDirectRouteProvider but this works only for ApiController. I want to use the same in MVC application [i.e. Controller]. so that when I access the AddorEditEntity method by following address www.localhost.com/Entity1/Add it should render the view.

Please advise whether this is possible.

0 Answers0