locked
New to .net Core from web forms and php background RRS feed

  • Question

  • User-1946112667 posted
    The more I look into Core the more I am taken over with the feeling that we are going back to spaghetti code. Mixing server side functionality into client side pages. Can someone please tell me I am wrong or how this is progress?
    Saturday, July 6, 2019 10:21 PM

All replies

  • User475983607 posted

    The more I look into Core the more I am taken over with the feeling that we are going back to spaghetti code. Mixing server side functionality into client side pages. Can someone please tell me I am wrong or how this is progress?

    In my experience spaghetti code is a design issue not the fault of a framework.  Is there anyway you can provide an example or point us to a ASP.NET tutorial?

    Saturday, July 6, 2019 11:18 PM
  • User1120430333 posted

    The more I look into Core the more I am taken over with the feeling that we are going back to spaghetti code. Mixing server side functionality into client side pages. Can someone please tell me I am wrong or how this is progress?

    I disagree, becuase what I see is seperation of concerns, inversion of control with dependency injection and loose coupling being  implemented in Core code development  that would otherwise be ignored  by developers that don't understand or know how to architect solutions. Core is putting it dead in their faces so they can't ignore it. :)

    And with VB.NET Core being released, they are also dragging the VB developers into it with them probably kicking and screaming  due to their undisciplined coding ways which is really not their fault. It's just that the VB developer never got the attention like the C# developers have enjoyed. . 

    https://msdn.microsoft.com/en-us/magazine/mt703433.aspx

    https://ardalis.com/new-is-glue

    https://www.c-sharpcorner.com/blogs/understanding-interfaces-via-loose-coupling-and-tight-coupling

    Sunday, July 7, 2019 12:14 AM
  • User71929859 posted

    mystrymaster

    The more I look into Core the more I am taken over with the feeling that we are going back to spaghetti code. Mixing server side functionality into client side pages.

    Not really. You can mix the server side functionality with client side pages in PHP, ASP.NET MVC, Web Forms (incline server codes with <% %>), Web Pages, J2EE if you want to too, not only .net Core. That's not a problem which .Net core introduced itself.

    mystrymaster

    Can someone please tell me I am wrong or how this is progress?

    Main idea behind introduction .NET core is it's platform independent, meaning you can host it anywhere, not only in Windows. Other than that, it comes with Dependency Injection container by default. There are many more. 

    Sunday, July 7, 2019 10:27 PM
  • User-1946112667 posted
    Yes you could mix server side code but with what I am seeing here you have to.
    Monday, July 8, 2019 12:40 AM
  • User475983607 posted
    If you are writing spaghetti code, then you are missing a concept. Again, share an example so we can provide assistance.
    Monday, July 8, 2019 9:49 AM
  • User-1946112667 posted
    https://www.w3schools.com/asp/razor_syntax.asp


    Monday, July 8, 2019 11:31 AM
  • User541108374 posted

    Hi,

    https://www.w3schools.com/asp/razor_syntax.asp

    That tutorial's more targeted towards ASP.NET webpages which was a thing around 2012 (and used WebMatrix as an IDE). Though you could write such code in .NET Core (Razor part of MVC), it's not really encouraged.

    Likely the closest in ASP.NET Core what you're looking for is Razor Pages: https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.2.

    Kris.

    Monday, July 8, 2019 11:54 AM
  • User753101303 posted

    Hi,

    Technically speaking you can put tons of C# code inside your Razor pages and the framework can't really prevent that (and AFAIK you can always misuse most if not all frameworks).

    Show maybe one of your own code sample. For example if your code embarks some logic to show stuff you could instead expose that as data and keep your view clean...

    Edit: have a look maybe at https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.2

    Monday, July 8, 2019 12:07 PM
  • User475983607 posted

    https://www.w3schools.com/asp/razor_syntax.asp


    The link is not an example of ASP.NET Core Razor pages.

    Again, spaghetti code is logic that a developer write.  Razor syntax does not promote spaghetti code.  If you are finding that you have a lot of business logic in Razor then you are writing spaghetti code and should move that logic to a controller action or better yet craft a service that is injected into the controller.

    Please see the ASP.NET Core fundamentals section in the ASP.NET Core reference docs.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-2.2&tabs=windows

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2

    Monday, July 8, 2019 12:39 PM
  • User-474980206 posted

    .net for razor pages are the most similar to php, where the html and code go into the same file (though razor pages supports a code behind page like web forms) .net core mvc is the framework that is most similar to any php mvc framework.
    Monday, July 8, 2019 2:13 PM
  • User-821857111 posted

    Can someone please tell me I am wrong

    Yes. You are wrong. Easy mistake to make, but as others have pointed out, you are not looking at ASP.NET Core. You are looking at ASP.NET Web Pages, which runs on the full .NET Framework. 

    Try this tutorial: https://www.learnrazorpages.com/razor-pages/tutorial/bakery. It's actually based on one of the sample sites that came with ASP.NET Web/Spaghetti Pages, and looks at how to migrate it to Razor Pages on .NET Core. 

    Tuesday, July 9, 2019 2:13 PM
  • User-1946112667 posted
    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    <h1>Welcome to Fourth Coffee!</h1>
    <div id="featuredProduct" class="row"> 
        <div class="col-sm-8">
            <img alt="Featured Product" src="~/Images/Products/@Model.FeaturedProduct.ImageName" class="img-fluid rounded"/>
        </div>
        <div id="featuredProductInfo" class="col-sm-4">
            <div id="productInfo">
                <h2>@Model.FeaturedProduct.Name</h2>
                <p class="price">$@string.Format("{0:f}", Model.FeaturedProduct.Price)</p>
                <p class="description">@Model.FeaturedProduct.Description</p>
            </div>
            <div id="callToAction">
                <a class="btn btn-danger order-button" asp-page="/order" asp-route-id="@Model.FeaturedProduct.Id" title="Order @Model.FeaturedProduct.Name">Order Now</a>
            </div>
        </div>
    </div>
    <div id="productsWrapper" class="row">
    @foreach (var product in Model.Products)
    {
        <div class="col-sm-3">
            <a asp-page="/order" asp-route-id="@product.Id" title="Order @product.Name">
                <div class="productInfo">
                    <h3>@product.Name</h3>
                    <img class="product-image img-fluid img-thumbnail" src="~/Images/Products/Thumbnails/@product.ImageName" alt="Image of @product.Name" />
                    <p class="description">@product.Description</p>
                </div>
            </a>
            <div class="action">
                <p class="price float-left">$@string.Format("{0:f}", product.Price)</p>
                <a class="btn btn-sm btn-danger order-button float-right" asp-page="/order" asp-route-id="@product.Id" title="Order @product.Name">Order Now</a>
            </div>
        </div>
    }
    </div>

    From that tutorial that you shared, that is exactly what PHP was when I was using it, huge hodge podge of server side blocks in the middle of HTML. 

    Tuesday, July 9, 2019 2:40 PM
  • User475983607 posted

    From that tutorial that you shared, that is exactly what PHP was when I was using it, huge hodge podge of server side blocks in the middle of HTML. 

    You misunderstand a major concept of the framework.  The Razor code shared above is a data interface for the UI.  In the old days of Classic ASP and PHP, data access code is often found within the markup and why this coding style was called spaghetti code.  If you take a closer look at the shared code you'll see there's no data access code in the markup.  The markup is taking the data fetched, usually from a DI service, and using it to craft HTML.

    Tuesday, July 9, 2019 3:18 PM
  • User-821857111 posted

    huge hodge podge of server side blocks
    Huge? Really?

    Are you comparing this to a client side approach like React or Angular perhaps? If so, you are comparing apples with oranges. If you generate your HTML on the server, you need a templating engine that enables you to specify where in the HTML the server-side values should be rendered. That's how server-side templating engines work. That's what Razor is.

    Tuesday, July 9, 2019 3:29 PM
  • User753101303 posted

    Not really what I expected for spaghetti code.

    A Razor page is just an easy way to write C# code that renders HTML markup. You still need to tell which data renders where and maybe loop on data so that you can render an HTML snippet for each item.

    In short the sole purpose is to render what the user will see and how data are fetched or what will happen on various actions is defined at its own place.

    Depending on your concern, your can still use the framework to futher enhance this. For example if you render this kind of "card collection" UI with a name, description and thumbnail you could turn that into your own component to write maybe something such as:

    <cards datalist="Model.Products"/>

    Tuesday, July 9, 2019 4:19 PM
  • User-1209166642 posted

    Sounds like you never programmed php.  What do you think blade is in laravel?  Answer php  Of course you will have server side language in the view, how else would you foreach and display the data, or table.

    Again, sounds like you do not know php, me I have used java, php, asp.net over the years, they are all the same.

    <td class="dname">{{ $row->dogname }}</td>
    <td class="dsex">{{ $row->sex }}</td>
    <td class="dogcomment">{{ $row->comments }}</td>

    Goodness, what do you think lines like:

    <td class="dogcomment">{{ $row->comments }}</td>

    is?  Well html with php (blade)

    Your post, sorry sounds offensive.  You should take the quality time to learn one of the languages prior to cutting a whole language down.

    .net core has "code behind" classes.  Have you even taken any "free" tutorials so instead of being confused, you would understand how things work.

    Compare to some .net core code:

    <th>@Html.DisplayNameFor(model => model.Pet[0].PetName)</th>
    <th>@Html.DisplayNameFor(model => model.Pet[0].DogPic) </th>
    

    ANY OF THE "BIG"  languages have to be in the html to "have the data displayed,

    Java, asp.net, php, coldfusion, ruby, etc, etc.

    What's tuff, is actually having to explain this stuff.  You on your own can go compare to some cakephp "baked" code.

    Wednesday, July 10, 2019 3:41 AM