2016-06-16 5 views
-1

Asp.Net MVC을 사용하여 새보기를 만들려고합니다. 내 컨트롤러의 각 변경 사항과 함께 내 뷰가 성공적으로 생성되었지만asp.net MVC 컨트롤러에서보기 만들기

뷰가 프로젝트에 포함되어 있지 않으므로 404 오류가 표시됩니다.

내가 이것을 실행하고 url/controller/Action을 작동시키려는 경우 포함 후 완벽하게 작동합니다.

내 코드

// 내 주요 목표 컨트롤러의 동작을보기를 생성하고 추가 할 다음과 같이 동적으로

[HttpPost] 
public ActionResult Index(Content model) 
{ 
    var fileName = model.Name; // validate to check the same name don't exist. 
    if (!System.IO.File.Exists(fileName)) 
    { 
     System.IO.File.Create(Server.MapPath("~/Views/Custom/"+fileName+".cshtml")); 
    } 
    //start Append data in custom controller 
    var lines = System.IO.File.ReadAllLines(Server.MapPath("~/Controllers/CustomController.cs")); 
    System.IO.File.WriteAllLines((Server.MapPath("~/Controllers/CustomController.cs")), lines.Take(lines.Length - 2).ToArray()); 
    //start Append data in custom controller 
    //System.IO.File.WriteAllLines((Server.MapPath("~/Controllers/CustomController.cs")), "public ActionResult'" + fileName + "'(){"); 
    string appendData = "public ActionResult "+ fileName+ "() \n { \n"; 
    appendData += "return View();"; 
    appendData += " \n } \n } \n }"; 
    System.IO.File.AppendAllText((Server.MapPath("~/Controllers/CustomController.cs")), appendData); 
    objcontext.objContent.Add(model); 
    objcontext.SaveChanges(); 
    ModelState.Clear(); 
    return View(); 
} 

어떻게 위치를 동적으로 파일을 포함하는 제발 도와주세요 새 페이지를 만드는 것입니다 I 수동 포함을 할 필요가 없습니다.

+2

왜이 작업을 수행 할 것입니다 방법 아래에서보세요? 무슨 엉망 이냐구 –

+0

나는 백엔드에서 새로운 페이지를 만들 수있는 권한을 관리자에게주고 싶다. –

+0

http://stackoverflow.com/questions/25765196/implementing-routes-for-dynamic-actions-in-mvc-4-0 –

답변

0

실행 중 어셈블리를로드하여 동적 컨트롤러를 구현할 수 있습니다. 그러나 CS 클래스를 작성하면 작동하지 않습니다.

http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/

+0

당신이 틀렸어. 새로운 관점을 만들고 싶습니다. 이를 위해 제공된 코드로 완벽하게 작동하는 컨트롤러에서 파일과 액션을 생성해야합니다. 내 응용 프로그램에보기가 포함되어 있지 않으므로 404가 표시됩니다. 실행 한 후 포함하면 작동합니다. –

관련 문제