简体   繁体   中英

How to return View into another project (modules) in .net core web application

I am setting up an architecture like modular programming, But I would like to return the view from one project to another project.

I had tried with the codes which i have found from,

  1. https://www.codeproject.com/Articles/1109475/Modular-Web-Application-with-ASP-NET-Core
  2. http://www.binaryintellect.net/articles/90d7323f-dcde-40d4-aa30-987bc8db1bf4.aspx

Project structure may like,

WebApplicaitonModule1
        Controllers
        Views
        Layouts

WebApplicationModule2
        Controllers
        Views
        Layouts

WebApplicationModule3
        Controllers
        Views
        Layouts

WebApplicationModule4
        Controllers
        Views
        Layouts

Models and DAL are different libraries

I need something like, how to redirect/return the View from Module1 to the view of Module2's Page?. is that possible in .net core applications. I am using .net core 2.1.

Because the I need to get the layout differently in Module wise.

My Imaginary scenario,
 - In WebApplicationModule2 we have a action like this and it will return the view.
public IActionResult Index()
        {
            model objmodel = new model();
            return View("~/Home/Index.cshtml",objmodel );
        }

 then after adding Project Dependency, 
 - from WebApplicationModule1

public IActionResult Index()
        {
            model objmodel = new model(); // same model return from another project.
            return View("~/WebApplicationModule2/Home/Index.cshtml",objmodel );
        }

How can we achieve this? or is there any other ways to do this? Any help is appreciate.

I think you can use redirect to route like this. I'm not know about your controller name so you need to pass in controller you want to redirect to and the model you want to pass in that controller method

return RedirectToRoute(new 
{ 
    controller = "", 
    action = "Index", 
    model = model
});

Please let me know if you need any help. Cheers

Add the child projects as Razor Class Library.

Add as Project reference( This will include all the reference files which used in the Razor class library project) or as an assembly reference( which contains only the .dll and view.dll)

and then it will be easily we can call the views( better if we added as Area wise),

In WebApplicationModule2 we have an action like this and it will return the view.

public IActionResult Index()
    {
        model objmodel = new model();
        return View("~/Home/Index.cshtml",objmodel );
    }

Add as project reference or assmebly reference then from WebApplicationModule1.

public IActionResult Index()
    {
        model objmodel = new model(); // same model return from another project.
        return View("~/WebApplicationModule2/Home/Index.cshtml",objmodel );
    }

the passing model should be the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM