2016-06-16 8 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(); 
} 

新しいページを作成することです//

ビューを作成し、コントローラ

にアクションを追加するため、次のように

私のコードはどこで動的にファイルをインクルードする方法を、私を助けてくださいです手動のインクルードを行う必要はありません。

+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が表示されます。 –

関連する問題