F
F
fryette2014-11-16 16:55:17
JSON
fryette, 2014-11-16 16:55:17

How to return Json files in MVC?

When changing the language on the site, the following request is sent
****/translate_(language).json
How can I intercept such requests and return json files?
After all, Url can be different (Home/translate_(language).json,
Home/screen/translate_(language).json).
the example was taken here
plnkr.co/edit/NX51vL4e7IEJqXjVe4nu
from the example you can see that the js code sends a request to the above link, maybe you can redirect the code to the controller I need which will return json files?)
I’m not friends with JS yet, but as they say ,really needed.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Mishin, 2014-11-18
@sergeysmishin

If you use the code from the example, then the URL to translate_(language).json files will be built relative to the current URL, as you wrote.
In order for the URL not to be built relative to the current address, you need to add '/' to 'translation_' (from the example). Then requests will go to a URL like http://run.plnkr.co/translation_(language).json and the example will not work.
In your case, the easiest option.

  1. Create a controller like :
    public class LanguageController : Controller
     {
             public ActionResult Index(string language)
              {
                [...]
              }
          }
  2. Change the line var languageFilePath = 'translation_' + language + '.json'; (from the example) to: var languageFilePath = '/language?/language='+ language;.
  3. Requests will be of the form: http[s]://your_site//language/language?language=(language)'.

Do not change anything, but create an HttpHandler that will process json files.
An example is here: msdn.microsoft.com/ru-ru/library/ms228090(v=vs.100...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question