Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[.NET MVC] Subactions + HttpPost gaat mis

Pagina: 1
Acties:

  • F.West98
  • Registratie: Juni 2009
  • Laatst online: 23-11 13:04

F.West98

Alweer 16 jaar hier

Topicstarter
Hallo,

Ik heb voor mezelf (adhv dit) een subaction attribuut gemaakt. Hiermee kan je dus een linkje zo maken:
controller/action/(subaction)/attributen

Nu heb ik een probleem met HttpPost.
Ik heb de volgende (sub)actions:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
        [Subaction("Apply")]
        [ActionName("Bedrijven")]
        public ActionResult Bedrijven_Apply() {

            return View("Bedrijven_Apply");
        }

        [HttpPost]
        [Subaction("Apply")]
        [ActionName("Bedrijven")]
        public ActionResult Bedrijven_Apply(ApplyBedrijfModel application) {
            return View("Bedrijven_Apply", application);
        }

Een GET Bedrijven/Apply gaat goed, maar zodra ik een post doe krijg ik de volgende melding:
The current request for action 'Bedrijven' on controller type 'AccountController' is ambiguous between the following action methods: [bovenste actie] [onderste actie]
Iemand een idee hoe dit op te lossen?

2x Dell UP2716D | R9 7950X | 128GB RAM | 980 Pro 2TB x2 | RTX2070 Super
.oisyn: Windows is net zo slecht in commandline als Linux in GUI


  • Sebazzz
  • Registratie: September 2006
  • Laatst online: 19:04

Sebazzz

3dp

Heb je geprobeerd explicitiet een HttpGetAttribute te geven aan je bovenste action method?

Je kan als alternatief AttributeRouting gebruiken.

[Te koop: 3D printers] [Website] Agile tools: [Return: retrospectives] [Pokertime: planning poker]


  • F.West98
  • Registratie: Juni 2009
  • Laatst online: 23-11 13:04

F.West98

Alweer 16 jaar hier

Topicstarter
Inmiddels al opgelost, maar dat AttributeRouting is ook erg interessant!

Ik vermoed dat het probleem komt dat mijn Subaction bij beide functies Bedrijven_Apply true geeft (want de juiste actie) en dat het dan mis gaat. Ik heb het opgelost door een extra (optioneel) attribuut te geven aan de Subaction, post.
Dan kijkt hij of de request en POST is. Als dat zo is geeft ie enkel true aan de POST action, anders niet.

Beter de code erbij (einde van de IsValidForRequest function:
C#:
1
2
3
4
        var post = controllerContext.HttpContext.Request.HttpMethod == "POST";
        
        // determine whether subaction matches
        return this.Name == subName && this.Post == post;


Even terug is dan de post als extra attribuut erbij gezet

2x Dell UP2716D | R9 7950X | 128GB RAM | 980 Pro 2TB x2 | RTX2070 Super
.oisyn: Windows is net zo slecht in commandline als Linux in GUI


  • D-Raven
  • Registratie: November 2001
  • Laatst online: 16-10 10:47
Volgens mij is dat niet nodig. Zoals Sebazzz ook al zegt. Zet eens het HttpGet attribuut op je eerste Bedrijven_Apply method.

Wat er waarschijnlijk gebeurd is dat de routing als eerste de (get) bedrijven apply tegenkomt, maar omdat die niet specifiek alleen GET requests accepteert, maar ook POSTS, ziet hij die als een POST method, + je feitelijke POST method krijg je dus 2 methods met dezelfde signature, waardoor je die foutmelding krijgt.