How to use url segmentation in asp.net mvc?

2

Good I have the following doubt, I want to know how I can work with the url, what I mean is the following, in PHP / Codeigniter, we worked in this way.

//URL= https://localhost/algo

<?php if($this->uri->segment(1)=='algo') {?>
//se carga el script a usar en esa vista
<script src="<?= base_url(); ?>js/algo.js"></script>
<?php } ?>

I do this on the masterpage. what I really want is to load a script or style depending on the view that I find to not load libraries that I will not use How could I do something like that, in asp.net mvc is it possible?

    
asked by Nik.Code 16.08.2018 в 19:12
source

2 answers

2

I could solve it in this way.

@if (Request.Url.AbsolutePath == "/Trabajador")
{
 @Scripts.Render("~/bundles/jsEQ")
}

What Request.Url.AbsolutePath does is return the remaining part outside the base url.

    
answered by 04.10.2018 в 16:28
0

Well, what you can do is create a folder in your project, name it Shared , in this new section you can create Views / Scripts that you can then implement / share with all the views you want without need to always write the same.

For example:

Folder Shared | _Vista1.cshtml

@Scripts.Render("~/bundles/datatable")
@Scripts.Render("~/bundles/datatable.default")
@Scripts.Render("~/bundles/datatable.buttons")

Or another example _Selector.cshtml :

<div class="form-group">
    @using (Html.BeginForm(null, null, FormMethod.Get, new { @class = "form-inline" }))
    {
        <div class="form-group">
            @Html.Label("Hasta")
            <input id="hasta" type="text" name="hasta" data-provide="datepicker" class="form-control" placeholder="Hasta" value="@ViewBag.Hasta.ToShortDateString()" />
        </div>
    }
</div>

Now in your View you only call these views through the attribute section Script and Html.Partial (Html Helpers) at the end of your view (if they are Scripts) and in case it is Content display you place it where you want .

Script:

@section scripts
{
 @Html.Partial("_Vista1")
}

Content:

@section content
{
    @Html.Partial("_Selector")
    @Styles.Render("~/content/bootstrap-multiselect")
}

I hope I have given you a hand.

Greetings, comment on any concerns.

    
answered by 16.08.2018 в 19:53