How to use url segmentation in mvc?


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 mvc is it possible?

asked by Nik.Code 16.08.2018 в 19:12

2 answers


I could solve it in this way.

@if (Request.Url.AbsolutePath == "/Trabajador")

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

answered by 04.10.2018 в 16:28

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


Or another example _Selector.cshtml :

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

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 .


@section scripts


@section content

I hope I have given you a hand.

Greetings, comment on any concerns.

answered by 16.08.2018 в 19:53