0

I'm learning Asp MVC. I've been doing WPF MVVM programs for two years already, but i also need to learn ASP which is a common language used in web development in my country as far as i know. And i have also knowledge in c# so i think adjusting will not be very hard, but i'm already facing a lot of problems in making my website work. I tried reading about ASP and MVC but i learn by doing things and from my mistake than reading it. So i decided to give it a try.

I created an EMPTY MVC project using Visual Studio Community Edition 2017

I already created the Layout Page and the First Controller and the First View and its totally working fine.

This is the screenshot

enter image description here

Then i create the second controller. Then the problem comes in.

I created a new controller named NewPostController and ADD View for it like this

enter image description here

But it create another folder with the name of the View and inside it is the view it created

enter image description here

I don't want it to organize that way.

So i dragged the NewPost.cshtml into the admin folder. Run the application then i received an error saying

The resource cannot be found.
Requested URL: /Admin/NewPost

I did a search for a solution but i can't solve the problem

I tried specifying the view name

        public ActionResult NewPost()
        {
            return View("~/Admin/NewPost");
        }

Most of the solution i read is specify the View Name. But i can't make it work. What are the things that i missed? Or not understand? Thank you.

Liam
  • 27,717
  • 28
  • 128
  • 190
classname13
  • 123
  • 3
  • 13
  • 3
    Just use view name like this: `return View("NewPost");`. No need to use full path of cshtml file. – Tetsuya Yamamoto May 14 '18 at 09:45
  • 1
    Why would you change MVC's natural architecture? If you want your `NewPost` action to be in the `Admin` controller folder, then just move your action from the `NewPostController` to the `AdminController` – Rafalon May 14 '18 at 09:47
  • Why are you changing the default structure? –  May 14 '18 at 09:47
  • 1
    If you want to use non-standard locations for your views I'd recommend you implement a [custom Razor view engine](https://stackoverflow.com/questions/9838766/how-do-i-implement-a-custom-razorviewengine-to-find-views-in-non-standard-locati) – Liam May 14 '18 at 09:48
  • @TetsuyaYamamoto i already tried it. but it doesn't work, i tried it like the one posted as answer here https://stackoverflow.com/questions/9896950/how-does-asp-net-mvc-relate-a-view-to-a-controller-action – classname13 May 14 '18 at 09:48
  • @TetsuyaYamamoto, That will not work because its in a different controller. –  May 14 '18 at 09:49
  • Your 404 error means that you have not moved the `NewPost` method to the `AdminController` (you need to move both the method and the view) –  May 14 '18 at 09:51
  • Hi @StephenMuecke because when i run the VIEW the HTML is like this http://localhost:52304/NewPost/NewPost I don't want it that way. Did i do it wrong? – classname13 May 14 '18 at 09:51
  • 1
    You need to move **both** the method and the view file (the `public ActionResult NewPost()` needs to be in `AdminController` and the `NewPost.cshtml` file needs to be in `/Views/Admin/` –  May 14 '18 at 09:52
  • @StephenMuecke can you provide a code snippet for that? Or you mean i need to create a new ActionResult in the AdminController with the name of NewPost – classname13 May 14 '18 at 09:53
  • 1
    @classname13 You should move `NewPost` method inside `AdminController` to get `/Admin/NewPost` URL. The `NewPostController` will create `/NewPost/NewPost` instead. – Tetsuya Yamamoto May 14 '18 at 09:53
  • Got it. Thank you so much. Stephen and tetsuya. Stackoverflow has lots of people willing to help newbie like me – classname13 May 14 '18 at 09:59

2 Answers2

3

MVC have sort of a naming convention where if your controller is named FooController then your views should be keep in a folder name Foo.

Inside this controller you will have your

public ActionResult <name of view>

name exactly the same as the view for easy referencing.

So when you have a view under the Foo Folder and the name of that cshtml file is Hello then inside the FooController, you have a

public ActionResult Hello(//parameter here){
    //body here
}

Hope you understand my explanation.

Also to answer your question. I'm assuming you want the NewPost.cshtml as part of the admin folder. Just add

public ActionResult NewPost()

to your admin controller and then you can use

localhost/admin/NewPost()

If i miss anything or any error, please comment hehe answered this in a bit of a rush

Stasis
  • 116
  • 7
1

Just move your NewPost action to your AdminController as such:

public class AdminController : Controller
{
    public ActionResult Dashboard()
    {
        return View();
    }

    // Here you go
    public ActionResult NewPost()
    {
        return View();
    }
}

This is default MVC structure if you want both Dashboard and NewPost views to be in the Admin folder

Rafalon
  • 4,450
  • 2
  • 16
  • 30