Mvc2.net how to remove surrounding route values

in my mvc2 application i have action link like

<%=Html.ActionLink("dash.board", "Index", pck.Controller,  new{docid ="",id = pck.PkgID }, new { @class = "here" })%>

docid is set to an empty string because I want to clear the surrounding docid value that is present in the request context. I looked at a lot of material on the Internet and even tried docid = String.Empty, but it does not solve the problem and gives me url like / controller / action / id? docid = x. I also write the route description as suggested here , but this also did not solve the problem. PLZ offer me a way to clear these route values โ€‹โ€‹coming from the request context using html.actionLink. I do not want to use html.routeLink

+3
source share
3

, ( ) - , , .

Route RouteWithExclusions, , / URL-.

, . , . MapRoute, .

+2

, , ASP.Net MVC. , , ActionFilters. MVC3, , . , , .

, Result ( , ) ActionLinks . , , () , .

public class ClearAmbientRouteValuesAttribute : ActionFilterAttribute 
{
    private readonly string[] _keys;

    public ClearAmbientRouteValuesAttribute(params string [] keys)
    {
        if (keys == null)
            _keys = new string[0];

        _keys = keys;
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        foreach (var key in _keys) {
            // Why are you sticking around!!!
            filterContext.RequestContext.RouteData.Values.Remove(key);        
        }
    }
}

// Inside your Global.asax
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new ClearAmbientRouteValuesAttribute("format"));
}

, -, . , .

+1

, , MVC5. , , , , ...

, , , , :

http://localhost:9999/DailyActivity/2014-01-01

,

http://localhost:9999/DailyActivity

http://localhost:9999/DailyActivity/Index

@Html.ActionLink("Test Link", "Index", "DailyActivity")

:

http://localhost:9999/DailyActivity/2014-01-01

:

@Html.ActionLink("Test Link", "Index", "DailyActivity", new { date = String.Empty }, null)

:

http://localhost:9999/DailyActivity?date=2014-01-01

, , , , , . ( ). :

@Html.ActionLink("Test Link", "/Index", "DailyActivity")

:

http://localhost:9999/DailyActivity/Index

@Html.ActionLink("Test Link", "/Index/..", "DailyActivity")

http://localhost:9999/DailyActivity

, , , , ...

EDIT:

Perhaps it would be better to do this (for the default action):

@Html.ActionLink("Test Link", String.Empty, "DailyActivity")

0
source

Source: https://habr.com/ru/post/1758916/


All Articles