Route.config of website in C # asp.net

1

I'm building a website. But there is one question that I can not solve. I have a Home Controller where I have all the Site Views. For example, I enter andwhenIclickononeoftheimagestogotoanotherView,, my URL does not appear as I wanted. I wanted it to appear - >

FootballGrassrootsViewCodeExcerpt:

<divclass="gallery">
        <a href="/Home/DOMOSlide">
            <img src="~/Imagens/Slide DS.jpg" alt="DOMO Slide DS">
        </a>
        <div class="desc">DOMO® Slide DS</div>
    </div>

Does it have to be on Route.config?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace SiteTESTE
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
    
asked by anonymous 16.05.2018 / 11:28

1 answer

0

Leave your RouteConfig.cs like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace SiteTESTE
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "RelvaFutebol",
                url: "Home/RelvaFutebol/DOMOSlide",
                defaults: new { controller = "Home", action = "DOMOSlide", id = UrlParameter.Optional }
            );          

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

Note that a new route has been added, it has the name RelvaFutebol and the URL to access it is Home/RelvaFutebol/DOMOSlide and will access Controler Home and Action DOMOSlide

The route Dafault should go to the end because it will always access the route that fits first, and in most cases Default is the that fits

As commented out, you can set to action to be dynamic

routes.MapRoute(
    name: "RelvaFutebolDinamica",
    url: "Home/RelvaFutebol/{action}",
    defaults: new { controller = "Home", action = "DOMOSlide", id = UrlParameter.Optional }
);       

Another tip is to raise your HTML to:

<div class="gallery">
    <a href="@Url.Action("DOMOSlide","Home")">
        <img src="~/Imagens/Slide DS.jpg" alt="DOMO Slide DS">
    </a>
    <div class="desc">DOMO® Slide DS</div>
</div>

With @Url.Action("DOMOSlide","Home") it will automatically create a link to the path of that controller , generating html with href="Home/RelvaFutebol/DOMOSlide"

You can see more at documentation and also in this page that explains about the subject (in English)

    
16.05.2018 / 15:37