Route.config of website in c# Asp.net

Asked

Viewed 517 times

1

I’m building a website. But there’s one issue I can’t resolve. I have a Home Controller, where I have all the Views of the site. For example, I enter nesta view and when I click one of the images to go to another View, por exemplo, my URL does not appear as I wanted. I wanted it to appear -> Exemplo

Excerpt from View Football Turf Code:

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

It will 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 }
            );
        }
    }
}
  • Miguel, put the images of the question itself, it is easier to view. Second, put your code, in particular the definition of the routes in global.asax.cs and an example of actions home, as RelvaFutebol

  • I have updated my question. global.asax.Cs has not changed since the beginning of the project.

  • In your project you must have a folder App_Start and inside a file RouteConfig.cs, if possible, add it to the question,

  • I’ve already added Route.config code

1 answer

0


Let yours RouteConfig.cs thus:

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 the Controler Home and the Action DOMOSlide

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

As commented, you can set to that the action 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 for:

<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 the @Url.Action("DOMOSlide","Home") automatically it will create a link to the route of that controller, generating a html with href="Home/RelvaFutebol/DOMOSlide"

You can see more on documentation and also in page who explains on the subject(in English)

  • Thank you, it worked! Just one more question, I have different types of grass for each sport, for example: in Relvafutebol I have Domoslide, Domovarioslide and Domoduraforce ie, I need to make for every kind of lawn right?

  • In this case in the last parameter you can add {action}, so will take the action passed, edited the question and added the example

  • Okay thank you! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.