c# - How to map 2 routes to the same URL? -


i want webpage different things depending on webpage accessed before was.

basically, know how make data ever-lasting persistence. however, method involves using query strings, don't want second user able access webpage query strings have been created before. so, trying around problem mapping multiple routes single url. possible in c# mvc?

specifically, whenever click link view(it can found below), route called first route has url search_history_page. meaning route corresponding action method not called. the_search_history_page

here route.config

using system.web.mvc; using system.web.routing;  namespace cbcm_audio_searcher {     public class routeconfig     {         public static void registerroutes(routecollection routes)         {             routes.maproute(                 name: "route_that_leads_to_the_home_page_as_the_first_page",                 url: "the_home_page",                 defaults: new { controller = "first_", action = "goes_to_the_home_page_as_the_first_page"}             );              routes.maproute(                 name: "route_that_leads_to_the_search_history_page_as_the_first_page",                  url: "the_search_history_page",               defaults: new { controller = "first_", action = "goes_to_the_search_history_page_as_the_first_page", id = 1}             );              routes.maproute(                name: "route_that_leads_to_the_search_history_page_from_the_home_page",                url: "the_search_history_page",                defaults: new { controller = "first_", action = "goes_to_the_search_history_page_from_the_home_page", id=0 }            );         }     } } 

here controller.

using system.web.mvc; using system.diagnostics;   namespace cbcm_audio_searcher.controllers {     public class first_controller : controller     {         public actionresult goes_to_the_home_page_as_the_first_page()         {              database_data_modifier_and_extractor  database_data_modifier_and_extractor=new  database_data_modifier_and_extractor();                if (database_data_modifier_and_extractor.checks_if_a_user_can_access_the_website() == false)             {                 return redirect("http://www.google.com");               }              else              {                 viewdata["user_id"]=database_data_modifier_and_extractor.user_id;                          return view("the_home_page");             }         }         public actionresult goes_to_the_search_history_page_as_the_first_page(string user_id,string dummyvariable, int id)         {             debug.writeline(id);              return view("the_search_history_page");          }         public actionresult goes_to_the_search_history_page_from_the_home_page(string user_id, string dummyvariable, int id)         {             debug.writeline(id);             return view("the_search_history_page");         }      } } 

here the_home_page_view.

@html.actionlink("your search results", "goes_to_the_search_history_page_from_the_home_page", "first_", new { user_id = viewdata["user_id"], dummyvariable = "a"}, null)   

my the_search_history_page view empty.

mvc has feature specify action name method. can specify name decorating method actionnameattribute , passing new action name arguement.

so whenever request comes /home/bar handed myactionmethod method processing.

public class homecontroller : controller {     [actionname("bar")]     public actionresult myactionmethod()     {         return content("foo");     }      [myactionselector]     [actionname("foo")]     public actionresult foo()     {         return content("foo");     }      [myactionselector]     [actionname("foo")]     public actionresult foo2()     {         return content("foo2");     } } 

when request comes in mvc methods based on routing table , actionnameattribute handle request. when mvc find more 2 method request throws ambiguousmatchexception.

from list of methods can specify method process request creating custom actionmethodselectorattribute. when actionmethodselectorattribute decorated method mvc execute isvalidforrequest before executing method return response. if isvalidforrequest return true, mvc execute method returning response. else mvc find other method match routing criteria.

to create own attribute have extend actionmethodselectorattribute class , override isvalidforrequest method. isvalidforrequest method has controllercontext , methodinfo input parameter. method may called multiple times based on how many method able fulfill request. each time methodinfo have different information method should process request(based on routing table/actionnameattribute).

public class myactionselectorattribute : actionmethodselectorattribute {     public override bool isvalidforrequest(controllercontext controllercontext, system.reflection.methodinfo methodinfo)     {         httprequestbase request = controllercontext.requestcontext.httpcontext.request;         // custom method selection logic goes here         // select method based on searched term         if (request.querystring["foo"] != null && methodinfo.name == "foo")         {             return true;         }         else if (request.querystring["foo2"] != null && methodinfo.name == "foo2")         {             return true;         }         return false;     } } 

in our case when request comes /home/foo mvc has 2 method foo , foo2 should process request. said earlier before executing method return response mvc invoke myactionselectorattribute.isvalidforrequest both of methods. illustration purpose grabbing querystring , checking

1) if foo present in query string , method foo return true (means allow execution foo method)

2) else if query string contains foo2 , method foo2 return true (means allow execution foo2 method)

3) else return false.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -